LeetCode 149. Max Points on a Line(直线上的点)
来源:互联网 发布:网络机顶盒 直播 编辑:程序博客网 时间:2024/05/13 21:14
原题网址: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; }}
0 0
- LeetCode 149. Max Points on a Line(直线上的点)
- [leetcode] Max Points on a Line 判断最多有多少个点在同一条直线上
- Max Points on a Line 同一条直线上最大数量的点
- 3.max-points-on-a-line 直线上的最多点
- 【九度】题目1548:平面上的点 && 【LeetCode】Max Points on a Line
- 186.Max Points on a Line-最多有多少个点在一条直线上(中等题)
- LeetCode 149. Max Points on a Line
- [leetcode] 149. Max Points on a Line
- leetcode 149. Max Points on a Line
- Leetcode 149. Max Points on a Line
- LeetCode 149. Max Points on a Line
- leetcode.149. Max Points on a Line
- LeetCode-149.Max Points on a Line
- leetcode 149. Max Points on a Line
- leetCode 149. Max Points on a Line
- [LeetCode] 149. Max Points on a Line
- LeetCode 149. Max Points on a Line
- [LeetCode]149. Max Points on a Line
- jq 石头人 真实 伤害 计算器
- 集合之Collection(1)
- Java 数字转换为汉语中人民币的大写
- hdu_1074_Doing Homework(状压DP)
- 欢迎使用CSDN-markdown编辑器
- LeetCode 149. Max Points on a Line(直线上的点)
- 文档结构图,思维导图,Mindmanager、XMIND、imindmap
- hadoop 配置步骤小结
- Find Minimum in Rotated Sorted Array II
- fir.im Weekly - 从零开始创建 Android 新项目
- 后台进入前台弹密码
- CSS行高——line-height
- SmartTabLayout切换无法回弹的问题
- ios HomeKit 开发指南