题目

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;
}
};

GitHub测试程序源码

LeetCode(149) Max Points on a Line的更多相关文章

  1. 【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 ...

  2. 【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 ...

  3. Leetcode(3)无重复字符的最长子串

    Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...

  4. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  5. 新概念英语(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, ...

  6. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  7. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  8. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  9. 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 ...

随机推荐

  1. 《springcloud 三》分布式配置中心

    Git环境搭建 使用码云环境搭建git服务器端 码云环境地址:https://gitee.com/majie2018 服务端详解 项目名称:springboot2.0-config_server Ma ...

  2. 故障案例:主从同步报错Fatal error: The slave I/O thread stops because master and slave have equal MySQL server

    https://blog.csdn.net/cug_jiang126com/article/details/46846031

  3. windows下使用MYSQL的mysqldumpslow进行慢日志分析

    1.首先安装好perl环境. 2.在dos环境中,切换到perl目录中,例如我的目录是 dos 命令 cd c:\Perl\bin 3.在此目录输入perl mysqldumpslow的路径\mysq ...

  4. CentOS7.5 搭建mycat1.6.6

    1.环境及版本 操作系统:   CentOS 7.5 数据库:MySQL 5.7.23 jdk:1.8.0_191 mycat:1.6.6.1 cat /etc/centos-release mysq ...

  5. java中两个map比较

    一 /** * 用map的keySet()的迭代器(性能效率较低) * */ public void compareMap1 (){ Map<String, String> m1 = ne ...

  6. jvm 内存dump、gc查看、线程死锁,jmap、jstack、jstat

    1. jstat     这个命令对于查看Jvm的堆栈信息很有用.能够查看eden,survivor,old,perm等heap的capacity,utility信息 对于查看系统是不是有能存泄漏以及 ...

  7. Java学习知识体系大纲梳理

    感悟 很奇怪,我怎么会想着写这么一篇博客——Java语言的学习体系,这不是大学就已经学过的课程嘛.博主系计算机科班毕业,大学的时候没少捧着Java教程来学习,不管是为了学习编程还是为了期末考个高分,都 ...

  8. 关于Kettle的事务和转换内步骤的顺序执行

    关于Kettle的事务和转换内步骤的顺序执行 近来有项目中遇到Kettle事务处理和转换内步骤顺序执行的问题.为此进行了研究,找到了一个解决办法. 在Kettle中,一个Job内的转换,缺省是顺序执行 ...

  9. Author: Jan Odvarko, www.janodvarko.cz

    /*  * Author: Jan Odvarko, www.janodvarko.cz */ FBL.ns(function() { with (FBL) { function HelloWorld ...

  10. Country roads take me home, to the place I belong.

    Country roads take me home, to the place I belong.故乡的路,带我回家吧,回到我期盼已久的归宿.