LeetCode(149) 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.
分析
求解一个二维平面上所有点中,位于同一直线上的最多点数。
首先想到的算法就是首先固定两个点求其斜率,然后在从剩余节点中计算该直线中的点,累计,比较、、、该方法时间复杂度要O(n^3),肯定不是最优解。
其实,可以略去一层循环,固定一个点,遍历剩余点,求每个斜率,借助一个map存储每个斜率上的点数,比较得到最大值。
斜率:
任意一条直线都可以表述为
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也在一条线上。
注意:
1. points中重复出现的点。
2. int maxNum = 0;
初始化,以防points.size() ==0的情况。
3. mp[INT_MIN] = 0;
保证poins中只有一个结点,还有points中只有重复元素时,mp中没有元素。这两种极端情况。
4. int duplicate = 1;
duplicate记录重复点的数量,初始化为1,是因为要把当前的点points[i]加进去。
5. 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)
AC代码
class Solution {
public:
int maxPoints(vector<Point>& points) {
if (points.empty())
return 0;
int size = points.size();
if (size < 3)
return size;
//记录最后在同一直线上的最多点数
int maxNum = 0;
//记录每条直线上的点数
map<float, int> line;
//固定一点,求其余另外所有点数构成的直线斜率
for (int i = 0; i < size; ++i)
{
line.clear();
line[INT_MIN] = 0;
//记录与当前节点的相同节点数
int common = 1;
for (int j = 0; j < size; ++j)
{
if (j == i)
continue;
//相同的两个点
else if (points[i].x == points[j].x && points[i].y == points[j].y)
{
++common;
continue;
}
else{
float k = (points[i].x == points[j].x) ? INT_MAX : (float)(points[i].y - points[j].y) / (points[i].x - points[j].x);
++line[k];
}//else
}//for
map<float, int>::iterator iter = line.begin();
for (; iter != line.end(); iter++)
{
if ((iter->second + common) > maxNum)
maxNum = iter->second + common;
}//for
}//for
return maxNum;
}
};
LeetCode(149) Max Points on a Line的更多相关文章
- 【LeetCode】149. Max Points on a Line
Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...
- 【leetcode】Max Points on a Line
Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...
- Leetcode(3)无重复字符的最长子串
Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- 新概念英语(1-49)At the butcher's
新概念英语(1-49)At the butcher's What does Mr. Bird like? A:Do you want any meat today, Mrs. Bird? B:Yes, ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
随机推荐
- IIS中的 Asp.Net Core 和 dotnet watch
在基于传统的.NET Framework的Asp.Net Mvc的时候,本地开发环境中可以在IIS中建立一个站点,可以直接把站点的目录指向asp.net mvc的项目的根目录.然后build一下就可以 ...
- mysql索引方式
/* 所有MySQL列类型可以被索引.根据存储引擎定义每个表的最大索引数和最大索引长度. 所有存储引擎支持每个表至少16个索引,总索引长度至少为256字节.大多数存储引擎有更高的限制. 索引的存储类型 ...
- 【.Net MVC4 connectionString设置】获取SQL server数据库的连接字符串
第一步:创建向导文件 在桌面创建一个txt文件,并将文件后缀改成“.udl”. 第二步:选择“提供程序”tab页 双击新创建的“.udl”文件,进入后选择“提供程序”tab页,选择“Micros ...
- Emacs中自动刷新dired缓冲区
Emacs中自动刷新dired缓冲区 在dired模式中,如果在不同buffer间切换,buffer不会自动更新,有时还需要手工按“g”键,比较麻烦,如下设置和代码能够在buffer切换和执行shel ...
- calibre电子书管理软件
软件介绍: Calibre 是电子书管理软件,支持 Amazon.Apple.Bookeen.Ectaco.Endless Ideas.Google/HTC.Hanlin Song 设备及格式,功能十 ...
- Jenkins访问路径配置自定义的相对路径
Jenkins安装时没有配置自定义的相对访问路径,例如配置的端口是29957,那访问路径就是http://localhost:29957.以下介绍把访问路径改成http://localhost:299 ...
- 转:error LNK2005: ...already defined in MSVCRTD.lib
编译错误: Error1error LNK2005: _CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.l ...
- IE浏览器已经卸载,但是桌面上的图标却无法删除的解决方案
第一步——win+R运行[regedit],打开注册表. 第二步——在注册表里面依次找到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVer ...
- Netbackup常用命令--bpdbjobs
1.大纲 bpdbjobs – 与 NetBackup 作业数据库进行交互 bpdbjobs [-report] [-M master_servers] [-ignore_parent_jobs] [ ...
- tpcc-mysql运行结果解读
前言 首先我们需要知道tpcc-mysql是干什么的.TPC-C是专门针对联机交易处理系统(OLTP系统)的规范,一般情况下我们也把这类系统称为业务处理系统.tpcc-mysql是percona基于T ...