Max Points on a Line(6)

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

如何判断点在同一条直线上呢?可以这么办。设定一个初始起点,计算剩余点到起始点直线的斜率,如果存在斜率相同的情况,则说明这几个点在同一条直线上。但是,还要考虑斜率不存在和重复点的情况。所以单独记录重复点的个数和斜率为正无穷的情况。最后取最大值应该是怎么个取法呢?应该是取 斜率相同的点数+重复点数  和垂直线的条数+重复的点数 的最大值,这个多想一会应该能明白。为了快速得出斜率值相同的直线条数,在本例中用到了map,只要给定斜率值,就能快速找到对应的关联值,方便快捷,不用自己再去写一层循环了。

说来都有一点惭愧,搞了很长时间终于完成,事后发现原因居然是因为if判断条件中误将==写成了=,真是有点伤怀啊!本来很快就能ac了!

现贴出代码!

 1 class Solution {
 2 public:
 3     int maxPoints(vector<Point> &points) {
 4         if(points.size()<=2)
 5             return points.size();
 6             int i,j;
 7             int coin;
 8             int h;
 9             int maxsize=-1;
10             double k;
11             for(i=0;i<points.size()-1;i++){
12                 map<double, int > point;
13                 map<double ,int >::iterator it;
14                 h=0;
15                 coin=1;//自己本身也算重复了 因为凡是组成直线,点数都是从2起步!
16                 for(j=i+1;j<points.size();j++){
17                     if(points[j].x==points[i].x&&points[j].y==points[i].y){
18                         coin++;
19                         continue;
20                     }
21                     else if(points[j].x==points[i].x){//就是这里刚开始把==弄成了=!
22                         h++;
23                         continue;
24                     }
25                     else if(points[j].y==points[i].y){
26                         k=0.0;
27                     }
28                     else{
29                         k=(double)points[j].y-points[i].y;
30                         k/=(double)(points[j].x-points[i].x);
31                     }
32                     it=point.find(k);
33                     if(it==point.end()){
34                         point.insert(map<double ,int>::value_type(k,1));
35                     }
36                     else{
37                         point[k]++;
38                     }
39
40                 }
41                 h+=coin;//h必须大于coin
42                 for(it=point.begin();it!=point.end();it++){
43                     int temp=it->second+coin;
44                     if(temp>maxsize)
45                         maxsize=temp;
46
47                 }
48                 if(h>maxsize)
49                     maxsize=h;
50
51             }
52
53             return maxsize;
54
55
56     }
57 };
时间: 2024-08-03 09:56:00

Max Points on a Line(6)的相关文章

【leetcode】Max Points on a Line(hard)☆

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 思路: 自己脑子当机了,总是想着斜率和截距都要相同.但实际上三个点是一条直线的话只要它们的斜率相同就可以了,因为用了相同的参照点,截距一定是相同的. 大神的做法: 对每一个点a, 找出所有其他点跟a的连线斜率,相同为同一条线,记录下通过a的点的线上最大的点数. 找出每一个点的最大连线通过的点数. 其

【leetcode】Max Points on a Line (python)

给定一个点,除该点之外的其他所有点中,与该点的关系要么是共线,要么就是共点,也就是两点重合. 共线有三种情况:水平共线,垂直共线,倾斜的共线.合并下这三种情况就是斜率存在的共线和斜率不存在的共线. 那么我们的任务就是针对每个点,找出与其共线的这些情况中,共线最多的点的个数. 注意:最终的结果别忘了加上共点的个数. class Solution: def maxPoints(self, points ): if len( points ) <= 1: return len( points ) ma

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]] 输出: 4 解释: ^ | | o |     o   o |      o |  o   o +------------------

Max Points on a Line (HASH TABLE

QUESTIONGiven n points on a 2D plane, find the maximum number of points that lie on the same straight line. 1ST TRY /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * };

Max Points on a Line leetcode java

题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 这道题就是给你一个2D平面,然后给你的数据结构是由横纵坐标表示的点,然后看哪条直线上的点最多. (1)两点确定一条直线 (2)斜率相同的点落在一条直线上 (3)坐标相同的两个不同的点 算作2个点 利用HashMap,Key值存斜率,Value存此斜率下的点的个数.同时考虑特殊情况,如

[LeetCode OJ] 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.

//定义二维平面上的点struct Point { int x; int y; Point(int a=0, int b=0):x(a),y(b){} }; bool operator==(const Point& left, const Point& right) { return (left.x==right.x && left.y==right.y); } //求两个点连接成的直线所对应的斜率 double line_equation(const Point&

LeetCode OJ - 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) 参考了网上的思路:枚举第一个点,用unordered_map来记录其余的点和这个点的斜率,若斜率相同则代表这些点在同一直线上.避免double问题,把斜率转化成化简

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. 两个点重合(即为同一个点) 2. 两个点的很坐标相同,即两天构成的之前垂直于X轴 [代码] /** * D

【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. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po