LeetCode-149.Max Points on a Line
2016-06-06 22:10
447 查看
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为空的判断
另外,根据参考的代码,循环变量i,j的取值不一样,也需要消化一下
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; }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解