[LeetCode]149. Max Points on a Line

限时加码!20+主流AI编程工具免费用 购周边加赠Coding Plan Lite,Claude Code、Cursor等即刻畅享,学习进阶更高效! 阅读详情

[LeetCode]149. Max Points on a Line

题目描述

这里写图片描述

思路

对每个点,将所有可能的直线存储在map中,并记录直线上对应的点,每次返回点最多的个数
踩的坑:
1. 开始考虑存直线需要 y = k * x + b, 即需存储k, b两个值,后来想想,通过同一点的直线,只需要k即可确定,相同的k值代表相同的直线
2. k值存在为无穷大的问题,即直线与y轴垂直的时候。
3. k值精度问题,如果保存的是k值,当数据过大时候k值会直接相同,因此选择保存的是类似分数的形式,对diffY和diffX求最大公约数,之后保存他们对应的最简分数,可以较为准确的区分

代码

#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <cmath>
#include <algorithm>
using namespace std;

struct Point {
    int x;
    int y;
    Point() : x(0), y(0) {}
    Point(int a, int b) : x(a), y(b) {}
};

class Solution {
public:
    int GCD(int a, int b) {
        while (b != 0) {
            int t = b;
            b = a % b;
            a = t;
        }
        return a;
    }


    int maxPoint(vector<Point>& points) {
        if (points.size() <= 2)
            return points.size();
        int res = 0;
        for (int i = 0; i < points.size() - 1; ++i) {
            map<pair<int, int>, int> pointCount;
            int vertical = 0, sameCount = 1;
            for (int j = i + 1; j < points.size(); ++j) {
                if (points[j].x == points[i].x && points[j].y != points[i].y)
                    ++vertical;
                else if (points[j].x == points[i].x && points[j].y == points[i].y)
                    ++sameCount;
                else {
                    //double k = (float)(points[j].y - points[i].y) / (float)(points[j].x - points[i].x);
                    //long double k = atan((long double)((long double)(points[j].y - points[i].y) / points[j].x - points[i].x));
                    //cout << k << endl;
                    int diffY = points[j].y - points[i].y, diffX = points[j].x - points[i].x;
                    int gcd = GCD(diffY, diffX);
                    diffY /= gcd, diffX /= gcd;
                    ++pointCount[make_pair(diffX, diffY)];
                }
            }
            res = max(res, vertical + sameCount);

            for (auto &p : pointCount) {
                res = max(res, p.second + sameCount);
            }
        }
        return res;
    }
};

int main() {
    vector<Point> points = { Point(0, 0), Point(94911151, 94911150), Point(94911152, 94911151) };
    Solution s;
    cout << s.maxPoint(points) << endl;

    system("pause");
}
149. Max Points on a Line 原文链接:https://leetcode.com/problems/max-points-on-a-line/?tab=Description Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 要求:求出平面n个点中,最多共线的点数。 阅读详情

相关推荐

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

直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上。 示例 1: 示例 2: 我的解题: 哈希法 思路:判断一个点是否在直线上,只需要一个起始点和斜率。用一个for循环遍历所有点作为起始点,然后遍历剩下的元素的斜率。因为斜率可能是小数,所以用求得最简约分的分数表示。将斜率存储在hashmap中,value存储出现次数。求得次数最高的点数+1即可 做法: 1、判断特殊情况,为空或者数组数量小于3 2、遍历数组,依次作为起始点。 3、遍历剩下的元素,求得最大公约数,求.

ALittleKnight的博客 380

149. Max Points on a Line[Hard](Leetcode每日一题-2021.06.24)

Problem Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line. Constraints: 1 <= points.length <= 300 points[i].length == 2 -10^4 <= xi, yi

Bryan要加油 889

LeetCode每日一题(149. Max Points on a Line)

这样整体的流程基本就完了, 但是还有个细节问题就是(xj - xi)可能为 0, 这样(yj - yi) / (xj - xi)就会 panic, 我们需要用一个特殊的值来保存这种无限大的斜率, 我们继续看题目的约束, 里面提到了-10^4

wangjun861205的博客 202

LeetCode 149. Max Points on a Line

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. 题目分析题目大意是给出平面上nn个点的坐标, 求出这其中最多有多少个点在同一条直线上。首先进行复杂度下界的分析。 首先由

Guo15331092的博客 391

LeetCode149 Max Points on a Line

题目: Givennpoints on a 2D plane, find the maximum number of points that lie on the same straight line. Example 1: Input: [[1,1],[2,2],[3,3]] Output: 3 Explana...

zhangjun62的博客 302

[leetcode]149. Max Points on a Line

/* * @lc app=leetcode id=149 lang=java * * [149] Max Points on a Line */ class Solution { public int maxPoints(int[][] points) { if(points == null || points.length == 0) return 0; ...

Belle_Chou的博客 205

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. 给一个点集,判断在一条直线上的点最多有多少个。 枚举每一个点,算其他点和它的斜率,然后用一个unordered_map记录斜率出现的次数。 注意处理好分母为0,和两点重合的情况。 也是

无名山丘,崛起成峰 893

【重要+细节】LeetCode 149. Max Points on a Line

LeetCode 149. Max Points on a Line Solution1: 参考花花酱:https://zxi.mytechroad.com/blog/geometry/leetcode-149-max-points-on-a-line/ count by slope Time complexity:O(n2)O(n2)O(n^2) Space complexity:...

Allenlzcoder的博客 527

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) 法二:遍历每一个点i {其他点j与该点i组成直线的斜率xl,斜率相同,统计加1----斜率相同又过同一个点i

le119126的专栏 609

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. 两个点重合(即为同一个点)

Harry Huang 1227

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

解法 太多坑了这题 只能依次记录每条直线上的点 需要解决的问题就是: 如何唯一地标识一条直线? 用三元组(A,B,C),要注意2点,一是最简约分,二是统一正负号。比如我就统一为第一个非0的数为正 另外还有一个坑: 如何处理重复点? 首先,重复点不能作为得到直线的两个端点 其次,重复点的重复次数也是答案的候选者之一 # Definition for a point. # class Point(ob...

lemonmillie的博客 331

Leetcode149. Max Points on a Line

题目地址: https://leetcode.com/problems/max-points-on-a-line/ 给定nnn个二维平面里点的坐标,问最多有多少个点在同一条直线上。 暴力O(n3)O(n^3)O(n3)方法可以参考https://blog.csdn.net/qq_46105170/article/details/112556694。下面介绍一个O(n2)O(n^2)O(n2)做法。 可以 import java.util.Arrays; import java.util.HashMap; i

数学、算法爱好者的博客 384

leetcode No149. Max Points on a Line

Question:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 求二维坐标中,最多共线的点数Algorithm遍历每个点,找到斜率一样的点,用哈希表存储大小 注意: 相同的点单独计算 在同一竖线上斜率无穷大,可以记为INT_MAXAccep

Dufre 988

149. Max Points on a LineLeetCode

问题描述 题目意思是求出在同一条直线上最多的点。 入的几个坑: 1.之前定义map<Point,int>变量,Point存处理后的横纵坐标,发现map并不能对Point这个key值自动排序,然后我错误的以为只要自己写个cmp排序规则就行了,但是发现在cmp中没办法取到value值。可以用pair代替。 2.不能在双重循环之外进行比较取最大值,因为如上面的第二个例子,那四个点最后存进...

今天好好优秀了么 328

LeetCode 149 Max Points on a Line

题目 煞笔题目,怪不得那么多人diss 两层for循环,它还疯狂卡精度,double的精度不够,必须long double class Solution { public: map<pair<long double,long double>,int> m; map<pair<long double,long double>,int> ...

weixin_30575309的博客 124
上一篇: [LeetCode]541. Reverse String II
下一篇: [LeetCode]7. Reverse Integer
charon____
博客等级 码龄11年 3粉丝 202原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值