[leedcode 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.

/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */
public class Solution {
    public int maxPoints(Point[] points) {
        //三层遍历,依次判断是否这些点在一条线路。注意判断两个相同的点的情况。
        //相同点的个数使用一个计数器dup,非相同但在一条直线上也有一个变量cur
        if(points==null||points.length<=0) return 0;
        int len=points.length;
        int dup=1;
        int cur=1;
        int res=0;
        for(int i=0;i<len;i++){
            dup=1;
            for(int j=i+1;j<len;j++){
                if(points[i].x==points[j].x&&points[i].y==points[j].y){//相同点
                    dup++;
                    continue;
                }
                for(int k=j+1;k<len;k++){
                    if(isSameLine(points[i],points[j],points[k])){
                        cur++;
                    }

                }
                if(res<cur+dup)res=cur+dup;
                cur=1;
            }
            if(res<dup)res=dup;//注意都是相同点的处理
            cur=1;
        }
        return res;
    }
    public boolean isSameLine(Point i,Point j,Point k){//相同点的判断,斜率相同
        if((i.x-j.x)*(i.y-k.y)==(i.x-k.x)*(i.y-j.y)) return true;
        else return false;

    }
}
时间: 2024-08-06 03:46:11

[leedcode 149] Max Points on a Line的相关文章

149. 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. /** * 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 Solutio

149. Max Points on a Line (Array; Greedy)

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() : x(0), y(0) {} Point(int a, int b)

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. 要求是输入n个坐标点,输出最多有多少个点在同一条直线上. 代码提交如下: 1 int Solution::maxPoints(vector<Point>& points) 2 { 3 if (points.size()<=2) 4 { 5 return points.size();

Java for 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. 解题思路: 本题主要需要考虑到斜线的情况,可以分别计算出过points[i]直线最多含几个点,然后算出最大即可,由于计算points[i]的时候,前面的点都计算过了,所以不需要把前面的点考虑进去,所以问题可以转化为过points[i]的直线最大点的个数,解题思路是用一个HashMap储存斜率,遍历p

【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. 解析:题目要求是给定n个2维的点,求出总共有多少个点在同一条直线上.由数学知识可知,给定三个点a,b,c,如果三个点在一条直线上,则a和b的斜率与c和d的斜率是相同的.用哈希表来做,针对每个点,求出其余所有点与它的斜率,找出最大值.然后遍历所有的点,返回最大值. 特别需要主意的几点:1)可能存

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. /** * 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 Solutio

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

给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ /** * 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 {

【leetcode】149 Max Points on a Line

题目说明 https://leetcode-cn.com/problems/max-points-on-a-line/description/ 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 解法1 首先遍历点,在第二层循环中使用map记录倾角斜率及其匹配次数. 斜率dx/dy(dx表示两点x轴的距离,dy表示两点y轴的差值)进行计算,但是这里有一种特殊情况,就是dy=0的情况,此种情况下点也在同一直线下,所以需要单独做判断. 还有一种情况就是如果点坐标一致,则不管其他点

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