LeetCode 149. Max Points on a Line(直线上的点)

二叉树随机漫步 元旦快乐! 学习到中本聪bitcoin的文章的calculation部分的时候,出现了binomial random walk,不是很明白什么是random walk。发现这个文章写的很好:https://blog.csdn.net/fireguard/article/details/16111603 随机漫步,也叫随机游走,最早是物理学中做"布朗运动"的微观粒子的运动形态来形成的一个模型,布朗运... 阅读详情

原题网址:https://leetcode.com/problems/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) {
        int max = 0;
        for(int i=0; i<points.length && i+max<points.length; i++) {
            Map<Double, Integer> counts = new HashMap<>();
            int originCounts = 1;
            int verticalCounts = 0;
            int notOriginMax = 0;
            for(int j=i+1; j<points.length; j++) {
                int rx = points[j].x-points[i].x;
                int ry = points[j].y-points[i].y;
                if (rx < 0) {
                    rx = -rx;
                    ry = -ry;
                }
                if (rx == 0 && ry == 0) {
                    originCounts ++;
                } else if (rx == 0) {
                    verticalCounts ++;
                } else {
                    Double slope = (double)ry/rx;
                    Integer count = counts.get(slope);
                    if (count == null) count = 1; else count ++;
                    counts.put(slope, count);
                    // System.out.printf("(%d,%d)-(%d,%d), slope=%f, count=%d\n", points[i].x, points[i].y, points[j].x,points[j].y, slope, count);
                    if (count > notOriginMax) notOriginMax = count;
                }
            }
            if (notOriginMax + originCounts > max) max = notOriginMax + originCounts;
            if (verticalCounts + originCounts > max) max = verticalCounts + originCounts;
        }
        return max;
    }
}

方法二:以其中一点为原点,另一点标准化之后作为key。

/**
 * 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 {
    private int gcd(int x, int y) {
        if (x < y) {
            int t = x;
            x = y;
            y = t;
        }
        while (y!=0) {
            int m = x % y;
            x = y;
            y = m;
        }
        return x;
    }
    public int maxPoints(Point[] points) {
        int max = 0;
        for(int i=0; i<points.length && i+max<points.length; i++) {
            Map<String, Integer> counts = new HashMap<>();
            String origin = "0,0";
            counts.put(origin, 1);
            int notOriginMax = 0;
            for(int j=i+1; j<points.length; j++) {
                int rx = points[j].x-points[i].x;
                int ry = points[j].y-points[i].y;
                if (rx < 0) {
                    rx = -rx;
                    ry = -ry;
                }
                if (rx != 0 && ry != 0) {
                    int gcd = gcd(rx, Math.abs(ry));
                    rx /= gcd;
                    ry /= gcd;
                } else if (rx != 0) {
                    rx = 1;
                } else if (ry != 0) {
                    ry = 1;
                }
                String key = rx + "," + ry;
                Integer count = counts.get(key);
                if (count == null) count = 1; else count ++;
                counts.put(key, count);
                // System.out.printf("(%d,%d)-(%d,%d), relative=(%d,%d)\n", points[i].x, points[i].y,points[j].x, points[j].y, rx, ry);
                if ((rx != 0 || ry != 0) && count > notOriginMax) notOriginMax = count;
            }
            int count = counts.get(origin);
            if (notOriginMax + count > max) max = notOriginMax + count;
        }
        return max;
    }
}

方法三:另一点作为Long类型的key。

/**
 * 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 {
    private int gcd(int x, int y) {
        if (x < y) {
            int t = x;
            x = y;
            y = t;
        }
        while (y > 0) {
            int t = y;
            y = x % y;
            x = t;
        }
        return x;
    }
    public int maxPoints(Point[] points) {
        if (points == null) return 0;
        if (points.length <= 2) return points.length;
        int max = 2;
        for(int i=0; i<points.length-1; i++) {
            int same = 1;
            int ox = points[i].x;
            int oy = points[i].y;
            Map<Long, Integer> counts = new HashMap<>();
            for(int j=i+1; j<points.length; j++) {
                int tx = points[j].x - ox;
                int ty = points[j].y - oy;
                if (tx == 0 && ty == 0) {
                    same ++;
                    continue;
                } else if (tx == 0) {
                    ty = 1;
                } else if (ty == 0) {
                    tx = 1;
                } else {
                    if (ty < 0 || (ty == 0 && tx < 0)) {
                        tx = -tx;
                        ty = -ty;
                    }
                    int g = gcd(Math.abs(tx), Math.abs(ty));
                    tx = tx / g;
                    ty = ty / g;
                }
                Long key = (((long)tx - Integer.MIN_VALUE) << 32) + ty - Integer.MIN_VALUE;
                Integer count = counts.get(key);
                if (count == null) count = 1; else count ++;
                counts.put(key, count);
            }
            if (same > max) max = same;
            for(Integer count: counts.values()) {
                if (count + same > max) max = count + same;
            }
            if (max >= points.length - i) return max;
        }
        return max;
    }
}

另一种实现:

/**
 * 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) {
        if (points == null) return 0;
        int max = 0;
        for(int i = 0; i + max < points.length; i++) {
            int original = 1;
            int vertical = 0;
            Map<Long, Integer> slopes = new HashMap<>();
            for(int j = i + 1; j < points.length; j++) {
                if (points[j].x == points[i].x && points[j].y == points[i].y) {
                    original++;
                } else if (points[j].x == points[i].x) {
                    vertical++;
                } else {
                    long slope = 1000000L * (points[j].y - points[i].y) / (points[j].x - points[i].x);
                    Integer count = slopes.get(slope);
                    if (count == null) {
                        count = 1;
                    } else {
                        count++;
                    }
                    slopes.put(slope, count);
                }
            }
            max = Math.max(max, original + vertical);
            for(int count : slopes.values()) {
                max = Math.max(max, original + count);
            }
        }
        return max;
    }
}


Python爬虫实战:股票历史数据抓取与量化回测全流程详解 我们以**东方财富网(eastmoney.com)和雪球网(xueqiu.com)**为主要目标,演示如何爬取A股历史行情。最常用的HTTP请求库,简单易用,适合静态页面数据请求。本文详细介绍了从股票历史数据抓取到简单回测的完整流程,代码示例基于最新技术,结构清晰,适合初学者和进阶者学习量化投资实战。希望对你入门Python爬虫和量化交易有所帮助! 阅读详情

相关推荐

AI大模型驱动Web UI自动化测试:Playwright与RobotFramework智能整合实践

Web自动化测试是软件质量保障的关键环节,其核心原理是通过脚本模拟用户操作,对Web应用界面进行功能验证。传统方法依赖固定的元素定位和脚本逻辑,在应对频繁的UI变更时面临维护成本高、脚本脆弱的挑战。随着AI大模型技术的发展,其强大的自然语言理解和代码生成能力为自动化测试带来了新的技术价值。通过将大模型的意图理解与代码生成能力,与Playwright强大的浏览器自动化执行引擎以及RobotFramework灵活的关键字驱动框架相结合,可以构建出能够理解自然语言指令、动态生成稳定操作脚本的智能测试系统。这种融合

weixin_28718769的博客 418

Leetcode(149)——直线上最多的点数

Leetcode(149)——直线上最多的点数 题目 给你一个数组 pointspointspoints ,其中 points[i]=[xi,yi]points[i] = [xi, yi]points[i]=[xi,yi] 表示 X-Y 平面上的一个点。求最多有多少个点在同一条直线上。 示例 1: 输入:points = [[1,1],[2,2],[3,3]] 输出:3 示例 2: 输入:points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] 输出:4 提示

KCDCY的博客 1156

从SD更新字库到FLASH,再用LCD显示

用Flash当字库存储载体。思路是先将要用到的字库文件存在SD卡,然后存储到Flash,LCD显示时候从Flash中读取 还挺顺利的,移植的原子的程序,自己稍加修改,主要是我的液晶屏函数,Flash读写函数,还有存储文件的flash地址不一样。一晚上搞定,大小字体都OK。挺高兴哈哈。这几天忙工作事情有点耽搁了学习进展嘿嘿 在text.c的showstr函数中char的字体颜色随意设置的,可以显...

weixin_30505751的博客 918

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

weixin_30616969的博客 255

新手必看:如何用PhaGCN2和vConTACT2搞定宏病毒组注释(附避坑指南)

本文为宏病毒组研究新手提供了使用PhaGCN2和vConTACT2进行病毒注释的完整实战指南。内容涵盖从Conda环境配置、软件安装、参数解析到结果解读的全流程,并重点分享了整合双工具进行交叉验证的策略与常见报错解决方案,旨在帮助研究者高效、准确地完成病毒分类注释工作。

j2k3l4的博客 810

英飞凌TC3xx实战:PWM中点触发ADC采集+DMA自动搬运完整配置流程(基于ILLD库)

本文详细介绍了在英飞凌TC3xx系列微控制器上使用ILLD库实现PWM中点触发ADC采集并通过DMA自动搬运数据的完整配置流程。该方案适用于电机控制、电源管理等需要高精度定时采集的场景,通过GTM生成PWM信号、ADC采集和DMA传输实现全硬件自动化流程,显著提升系统效率。

weixin_42536058的博客 541

max points on a line

文章目录尝试一尝试2结论 题目内容 意思:在一个二维平面上有n个点,求出现在同一直线上的点(就是那n个点)的最大个数。 尝试一 结果:case通过率为7.69% 代码: /** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * ...

wobushixiaobailian的博客 255

RK3588, FFmpeg 拉流 RTSP, mpp 硬解码转RGB

RK3588, FFmpeg 拉流 RTSP, mpp 硬解码转RGB

J。 2347

LeetCode Max Points on a Line

题意:给定一些二维坐标,求多少个点在同一条直线上 解法:明显的极角排序,卡了几下,发现这道题目比uva 270多了几种情况。 1、先说这道题目的极角排序:O(n)遍历一遍,枚举极角排序的中心点。然后构造向量,进行极角排序。排序结束,根据差乘判断(假设只有一个中心点,即任意两个点坐标都不同),有多少个点在一条直线上。即为返回值ret。 2、题目可能有重复点。这个就需要我们把重复的点找出来。极角

miss_snow_m的专栏 461

EDKII环境配置完整版

参考博客【EDK2 UDK2018的环境搭建】:https://blog.csdn.net/qq_39155263/article/details/82965744下载EDK2 1.直接从https://github.com/tianocore/edk2/ 网站下载.zip压缩文件 2.运用svn 从http://svn.code.sf.net/p/edk2/code/trunk/edk2edk2下载 3.安装Visual Studio2017 安装其他Tool 1.IASL 从https..

tianpu2320959696的博客 1864

YOLOv12 改进 | Backbone改进 3

将CSP中的简单拼接改为可学习的特征选择门控,动态决定哪些特征流向下一层,同样再写这篇。:相比静态CSP的固定50/50分割,门控机制可学习0.3:0.7等最优比例。在输入端加入轻量SR模块,对下采样后的特征进行超分恢复,提升小目标检测能力。(小目标): 从 0.51 AP → 0.63 AP (+24%):采用ESPCN的高效亚像素卷积,避免传统插值的信息损失。:从通道级(SE)到空间级(Spatial)的多粒度控制。:结合浅层细节和深层语义,恢复小目标的纹理信息。

u012133341的博客 846

Max Points on a Line(leetcode)

题目:

u010585135的专栏 508

2025年下半年网络工程师案例分析真题及答案解析

2025年下半年网络工程师案例分析真题及答案解析

鹿鸣天涯 1108

在同一直线上最多点的数量 c语言,[LeetCode] 149. Max Points on a Line 共线点个数

Givennpoints on a 2D plane, find the maximum number of points that lie on the same straight line.给一个由n个点组成的2D平面,找出最多的同在一条直线上的点的个数。共线点的条件是斜率一样,corn case:点相同;x坐标相同。Java:public class Solution {public i...

weixin_35976993的博客 274

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

LeetCode | 149. Max Points on a Line求多个点里面在一条直线上的点最多有多少个难题

Given n points on a 2D plane, find the maximumnumber of points that lie on the same straight line. 感觉我的想法太复杂了,不过暂时也只想得到这个方法, 我的方法大概的原理是两点决定一条直线,首先遍历原点数组,将其中的相同的点复制到另外一个点数组里面去,这样另外一个点的数组就不会有重复的点, 然后

u012737193的博客 909

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard

题目: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. 这道题需要稍微转变一下思路,用斜率来实现,试想找在同一条直线上的点,怎么判断在一条直线上,唯一的方式也只有斜率可以完成,我开始没想到,后来看...

weixin_34360651的博客 132

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

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的博客 399

Max Points on a Line -- LeetCode

原题链接: http://oj.leetcode.com/problems/max-points-on-a-line/  这道题属于计算几何的题目,要求给定一个点集合,是求出最多点通过一条直线的数量。我的思路是n个点总共构成n*(n-1)/2条直线,然后对每条直线看看是有多少点在直线上,记录下最大的那个,最后返回结果。而判断一个点p_k在p_i, p_j构成的直线上的条件是(p_k.y-p_i.

Code Ganker 1万+

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 = ax + b 假设,有两个点(x1,y1), (x2,y2),如果他们都在这条直线上则有 y1 = kx1 +b y2 = kx2 +b 由此

ZkvIA的博客 2万+

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

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

lemonmillie的博客 333

[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的博客 207

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 1230

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的专栏 613

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的博客 307

[leetcode] 149. Max Points on a Line 解题报告

题目链接:https://leetcode.com/problems/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. 思路:又是一个折磨人的题,蛋疼呐!自己写了好久,就是在hash表中

小榕流光的专栏 1518

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,和两点重合的情况。 也是

无名山丘,崛起成峰 895
上一篇: LeetCode 150. Evaluate Reverse Polish Notation(计算后缀表达式)
下一篇: LeetCode 148. Sort List(链表排序)
jmspan
博客等级 码龄11年 99粉丝 431原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值