Leetcode: Max Points on a Line

STM32F103单片机驱动TM1637数码管显示模块 最近项目中需要用到数码管显示,于是买了一个TM1637芯片驱动的四位数码显示模块,现将调试过程记录一下,方便以后参考。 使用的单片机是STM32F103C8T6最小系统 使用的数码管模块是TM1637四位数码管显示模块 实际运行效果 下面先看一下TM1637和数码管连接的具体线路图 实际使用的模块没有带按键,只用了4个数码管,模块和单片机连接只需要4根线VCC、GND、CLK、DIO。芯片和单片机通信使用的是I2C总线,下面就来说一下如何... 阅读详情

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

分析:

任意一条直线都可以表述为

y = ax + b

假设,有两个点(x1,y1), (x2,y2),如果他们都在这条直线上则有

y1 = kx1 +b

y2 = kx2 +b

由此可以得到关系,k = (y2-y1)/(x2-x1)。即如果点c和点a的斜率为k, 而点b和点a的斜率也为k,可以知道点c和点b也在一条线上。

取定一个点points[i], 遍历其他所有节点, 然后统计斜率相同的点数,并求取最大值即可

/**
 * Definition for a point.
 * struct Point {
 *     int x;
 *     int y;
 *     Point() : x(0), y(0) {}
 *     Point(int a, int b) : x(a), y(b) {}
 * };
 */
class Solution {
public:
    int maxPoints(vector<Point> &points) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
		unordered_map<float,int> mp;
		int maxNum = 0;
		for(int i = 0; i < points.size(); i++)
		{
			mp.clear();
			mp[INT_MIN] = 0;
			int duplicate = 1;
			for(int j = 0; j < points.size(); j++)
			{
				if(j == i) continue;
				if(points[i].x == points[j].x && points[i].y == points[j].y)
				{
					duplicate++;
					continue;
				}
				float k = points[i].x == points[j].x ? INT_MAX : (float)(points[j].y - points[i].y)/(points[j].x - points[i].x);
				mp[k]++;
			}
			unordered_map<float, int>::iterator it = mp.begin();
			for(; it != mp.end(); it++)
				if(it->second + duplicate > maxNum)
					maxNum = it->second + duplicate;
		}
		return maxNum;
    }
};
注意:

0、points中重复出现的点。

1、int maxNum = 0;

初始化,以防points.size() ==0的情况。

2、mp[INT_MIN] = 0;

保证poins中只有一个结点,还有points中只有重复元素时,mp中没有元素。这两种极端情况。

3、int duplicate = 1;

duplicate记录重复点的数量,初始化为1,是因为要把当前的点points[i]加进去。

4、float k = points[i].x == points[j].x ? INT_MAX : (float)(points[j].y - points[i].y)/(points[j].x - points[i].x);

计算斜率,如果直线和y轴平行,就取INT_MAX,否则就取(float)(points[j].y - points[i].y)/(points[j].x - points[i].x)

一开始把(float)(points[j].y - points[i].y)/(points[j].x - points[i].x)写做(float)((points[j].y - points[i].y)/(points[j].x - points[i].x))一直就不对,后来才想明白,注意注意!



参考:

http://fisherlei.blogspot.com/2013/12/leetcode-max-points-on-line-solution.html

别让 Claude Code 一直问你“能不能执行“:权限配置完全指南 Claude Code 提供了一套精细的权限配置机制,配置好权限设置,可以提速不少。这个小技巧需要掌握一下。 阅读详情

相关推荐

详细介绍:国产操作系统银行麒麟V10的下载和安装

银河麒麟是由。银河麒麟已经发展为银河麒麟服务器操作系统、桌面操作系统、嵌入式操作系统、麒麟云、操作系统增值产品为代表的产品线。为攻克中国软件核心技术“卡脖子”的短板,银河麒麟建设自主的开源供应链,发起中国首个开源桌面操作系统根社区openKylin,银河麒麟操作系统以openKylin等自主根社区为依托,发布最新版本。

lz17267861157的博客 1万+

Points on Line(CF-252C)

Problem Description Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so ...

Alex_McAvoy的博客 1011

机器学习-深嵌交叉验证(Nested Cross-Validation)

Section I: Code and Analyses 第一部分:代码 from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC from...

Santorinisu Blogs 2727

Max Points on a Line(直线上最多的点数)

给定一个二维平面,平面上有n个点,求最多有多少个点在同一条直线上。 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | o +-------------> 0 1 2 3 4 示例2: 输入: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]...

weixin_30616969的博客 254

从X86到ARM:将VINS-Fusion-GPU和ego-planner迁移到Jetson Orin-NX时,我踩过的那些‘环境差异’坑

本文详细介绍了将VINS-Fusion-GPU和ego-planner从X86平台迁移到Jetson Orin-NX(ARM架构)时遇到的环境差异问题及解决方案。内容涵盖ARM与X86的架构差异、依赖管理、性能调优和硬件接口适配,特别强调了CUDA在ARM平台上的优化配置和内存访问技巧,帮助开发者高效完成跨平台迁移。

weixin_42647395的博客 127

告别玄学调参:手把手教你用20元件模型搞定GaN HEMT高频仿真(附S参数提取避坑指南)

本文详细介绍了如何使用20元件模型优化GaN HEMT高频仿真,解决传统16元件模型在10GHz以上频段的误差问题。通过S参数提取避坑指南和实战案例,帮助工程师精准建模分布式电极效应和寄生反馈路径,提升射频电路设计效率。

weixin_42523693的博客 445

max points on a line

文章目录尝试一尝试2结论 题目内容 意思:在一个二维平面上有n个点,求出现在同一直线上的点(就是那n个点)的最大个数。 尝试一 结果:case通过率为7.69% 代码: /** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * ...

wobushixiaobailian的博客 255

YOLO11 改进系列 | 引入2023年 MLCA 的 C3k2_MLCA 模块,近零参数开销的本地-全局通道注意力,适合检测与分割特征增强

本文提出将MLCA(混合局部通道注意力)模块嵌入YOLOv11的C3k2框架中,形成C3k2_MLCA改进结构。该模块通过5×5局部池化和1×1全局池化双路融合,结合ECA式Conv1d生成通道注意力,在几乎不增加参数量的前提下(仅增加66个参数),实现了局部空间信息与全局通道统计的联合建模。相比传统SE和ECA注意力,MLCA保留了空间分布信息,更适合实例分割等需要精细定位的任务。实验表明改进后模型参数量仅增加0.002%,计算量基本不变,是一种高效的注意力增强方案。

qq_44224801的博客 95

LeetCode Max Points on a Line

题意:给定一些二维坐标,求多少个点在同一条直线上 解法:明显的极角排序,卡了几下,发现这道题目比uva 270多了几种情况。 1、先说这道题目的极角排序:O(n)遍历一遍,枚举极角排序的中心点。然后构造向量,进行极角排序。排序结束,根据差乘判断(假设只有一个中心点,即任意两个点坐标都不同),有多少个点在一条直线上。即为返回值ret。 2、题目可能有重复点。这个就需要我们把重复的点找出来。极角

miss_snow_m的专栏 461

RTT Studio和Cubemx联合开发

1. RTT studio创建工程 创建工程 ## 创建完成以后的目录结构 2. 配置CubuMx 双击cubumx的图标打开CubeMx 配置时钟 生成代码 构建后的代码结构 编译代码满屏错误:不要慌 3. 新增脚本 新建scons脚本文件 SConscript 脚本内容如下 import os from building import * cwd = GetCurrentDir() src = Glob('*.c') # add cubemx drivers src = Split('''

Car12 1573

电磁兼容(EMC):一文读懂过电应力(EOS)

过电应力(EOS)是指元器件承受的电流或电压应力超过其允许的最大范围。产品的电磁兼容(EMC)失效的一种常见模型为过电应力(EOS),也是电子产品失效的最常见的情况之一。过电应力基本与半导体器件相关,EOS事件包括有:静电放电(ESD)、雷击、系统瞬态、电磁脉冲、充电等。本文将详细讨论一下产品设计中如何关注EOS设计。

linkking27的博客 6494

Max Points on a Line(leetcode)

题目:

u010585135的专栏 508

2022中山大学计算机技术专硕考研初试、复试经验帖

个人简介 推荐几个我自己感觉对考研非常有帮助的小助手吧。可以帮助节省时间! 考研时间规划总览 初试篇 数学 英语 政治 408 复试篇 机试 笔试 面试 关于选导师 个人感言

zik的博客 1万+

LeetCode: Max Points on a Line

这个设计test case是个好题目。本来的想法是以斜率为key,做hashtable计数,试了几次行不通。似乎改造下这个方案也是可行的。 后来干脆按照line来计数。最主要的是点重复这个事情不好弄,干脆吧点集预处理下。发现leetcode论坛里有个类似的思路,不过那个解法错了。 代码有点丑,如下: struct Point { int x; int y; Point() :

rreference 336

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

LeetCode: Max Points on a Line [149]

【题目】 Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 【题意】 给定一堆点,要求找出一条之前上的最大点数 【思路】 没什么好的方法,从每个点P出发,遍历所有的情况 从每个点P出发,斜率相同的点即为统一之前上的点 注意两种特殊情况: 1. 两个点重合(即为同一个点)

Harry Huang 1230

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

LeetCode:Max Points on a Line

Q: Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 思路:n个点,最多形成 (n-1)! 条线段,利用2个点p[i],p[j](j=i+1)可以得到它们所有的所属直线的斜率.以该斜率为key, Set为value,set中存放的是构成

Leo's Blog 1127

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

LeetCode OJ:Max Points on a Line

Max Points on a Line  Total Accepted: 3110 Total Submissions: 31616My Submissions Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

王川的私房菜 1055

LeetCode:149 Max Points on a Line

描述: Givennpoints on a 2D plane, find the maximum number of points that lie on the same straight line. Example 1: Input: [[1,1],[2,2],[3,3]] Output: 3 Explanation: ^ | | o | o | o +...

Sword1996的博客 355

Leetcode: Max Points on a Line 分析理解

题目大意:给定一系列二维空间内的点,找出zh

Amy的学习之路 813

LEETCODE: Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 居然是图像处理那个求直线算法的原理的简化版! /** * Definition for a point. * struct Point { * int x; *

博客已退休 408

LeetCode149:Max Points on a Line

题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 解题思路: 1,在所有点中选定一个点作为中心点,然后再求剩下的点到该中心点的斜率,如果斜率相同的点表示在同一直线上 2,如果剩下点中有与中心点相同的点,则记下相同点的个数,然...

diehuojiang5959的博客 166

leetcode 149: Max Points on a Line

I couldn't think of a specific way to solve the problem until I learned the idea from others. First, choose the point one by one and make it the start point. Then, choose another point after that poin

mikefan1991的博客 238

牛客网LeetCode算法题详解:max-points-on-a-line

欢迎访问我的个人博客(点击进入) max-points-on-a-line 1.题干: 对于给定的n个位于同一二维平面上的点,求最多能有多少个点位于同一直线上 Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 给出的数据结构: /** * Defi...

weixin_43265151的博客 268
上一篇: Leetcode: Evaluate Reverse Polish Notation
下一篇: LightInthebox 2013校招笔试题--2012年某月某日考试
「已注销」
博客等级 码龄18年 586粉丝 234原创
评论 9
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值