LeetCode-149.Max Points on a Line
来源:互联网 发布:奇迹暖暖换装游戏网络 编辑:程序博客网 时间:2024/04/25 20:19
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.
开始除了暴力破解没想到什么解决方案,暴力破解就是随机取两个点,判断其他的点在这两个点构成的直线上。代码比较繁琐就不实现了。
后来参考http://blog.csdn.net/linhuanmars/article/details/21060933
直接判断每一个点和其它点构成的斜率,然后统计同斜率的个数就好。
存在几个陷阱
1、当x1==x2,即斜率的无穷大时的处理。代码使用int.MaxValue,我觉得存在点问题:如果p1(1,-1),p2(2,int.MaxValue-1),那么这个两个点也是的斜率k就是int.MaxValue。比如输入是[[1,-1],[1,0],[2,2147483646]]这三个点,正确输出应该是2,但是当前解决方案却输出为3。
同时还发现一个问题,提交参考博文中的代码中,用以上测试用例的Expected answer=2,但是也能通过
而使用我的C#代码提交,用以上测试用例的Expected answer=3,也能通过。同时其它语言的Expected answer都是2,有鬼...
2、当点重合时
3、当全部点重合时,hashtable为空的判断
/** * Definition for a point. * public class Point { * public int x; * public int y; * public Point() { x = 0; y = 0; } * public Point(int a, int b) { x = a; y = b; } * } */public class Solution { public int MaxPoints(Point[] points) { int n = points.Length, max = 2, duplicate; if (n < 2) return n; double k; Hashtable table = new Hashtable(); for (int i = 0; i < n; i++) { duplicate = 0; for (int j = 0; j < n; j++) { if (i != j) { if (points[i].x == points[j].x && points[i].y == points[j].y) duplicate++; else { if (points[i].x == points[j].x) k = int.MaxValue; else k = (double)(points[i].y - points[j].y) / (points[i].x - points[j].x); if (table.Contains(k)) table[k] = (int)table[k] + 1; else table.Add(k, 2); } } } if (table.Count == 0) return n; foreach (int val in table.Values) max = Math.Max(max, val + duplicate); table.Clear(); } return max; }}
另外,根据参考的代码,循环变量i,j的取值不一样,也需要消化一下
public int MaxPoints(Point[] points) { int n = points.Length, max = 2, duplicate; if (n == 0) return 0; double k; Hashtable table = new Hashtable(); for (int i = 0; i < n-1; i++) { duplicate = 0; for (int j = i+1; j < n; j++) { if (points[i].x == points[j].x && points[i].y == points[j].y) duplicate++; else { if (points[i].x == points[j].x) k = int.MaxValue; else k = (double)(points[i].y - points[j].y) / (points[i].x - points[j].x); if (table.Contains(k)) table[k] = (int)table[k] + 1; else table.Add(k, 2); } } if (table.Count == 0) return n; foreach (int val in table.Values) max = Math.Max(max, val + duplicate); table.Clear(); } return max; }
0 0
- 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
- [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
- 单例模式
- ZOJ 3435 Ideal Puzzle Bobble(gcd(i,j,k)=1/莫比乌斯反演)
- Java Enumeration接口
- 机器学习自学指南
- 09-Hive查询操作Distributed by 和sort by
- LeetCode-149.Max Points on a Line
- web service发布及调用
- Linux设置环境变量小结:设置永久变量&临时变量 全局变量&局部变量
- C++中类的6个默认成员函数
- Spring:协调作用域不同步的Bean
- 【Spring4揭秘 基础5】BeanDefinition及读取、注册
- 拉链表
- iOS-监听UITextField值改变事件
- BigDecimal处理金额