题目

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. NetCore1.1+Linux

    NetCore1.1+Linux部署初体验   1.环境准备 Centaos7+Win10 虚拟机 Win10安装VS2017 https://www.asp.net/downloads注意勾选下.N ...

  2. 关于FutureTask的探索

    之前关于Java线程的时候,都是通过实现Runnable接口或者是实现Callable接口,前者交给Thread去run,后者submit到一个ExecutorService去执行. 然后知道了还有个 ...

  3. CSS中的定位机制

    CSS3 中有三种定位机制 : 普通文档流 (text)| 浮动(float) | 定位(position) 普通文档流 就是CSS中默认的文本文档 普通流中,元素位置由文档顺序和元素性质决定,块级元 ...

  4. Vue的computed和methods区别

    1,computed里面定义的方法是以属性的方式(当然也可以以函数调用的方式)出现在html里面,而methods里面定义的方法是以函数的方式: 2,computed依赖于data里面的数据,只有相关 ...

  5. gitlab api批量操作 批量添加用户

    import os,time import requests,json # def downloadFile(name, url): # headers = {'Proxy-Connection': ...

  6. 文末两大福利 | 微软Inspire大会全接触:微软发布Microsoft 365......

    在7月11日举行的“Inspire年度合作伙伴大会”上 ,微软首席执行官萨提亚·纳德拉发布了Microsoft 365. 它包含了:Office 365.Windows 10和企业移动性+安全性(En ...

  7. python 之 re 模块

    re模块下的常用方法 1.findall:返回所有满足匹配条件的结果,放在列表里. import re # 查找数字 result = re.findall('\d+','nizhidao 123 w ...

  8. windows7桌面小工具打不开的解决方案

    将任务管理器中的sidebar.exe结束任务: 将C:\Users\用户名\AppData\Local\Microsoft\Windows Sidebar下的settings.ini的文件名修改为任 ...

  9. 检查windows端口被占用

    开始---->运行---->cmd,或者是window+R组合键,调出命令窗口 输入命令:netstat -ano,列出所有端口的情况.在列表中我们观察被占用的端口,比如是49157,首先 ...

  10. python基础教程总结10——文件

    1.打开文件 open(name[mode[,buffing])    参数:  文件,模式,缓冲 1)name: 是强制选项,模式和缓冲是可选的 #如果文件不在,会报下面错误1 >>&g ...