LeetCode-Max Points on a Line
2014-07-28 12:49
344 查看
Given n points
on a 2D plane, find the maximum number of points that lie on the same straight line.
Solution:
Code:
<span style="font-size:14px;">/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
class Solution {
public:
int maxPoints(vector<Point> &points) {
const int length = points.size();
if (length == 0 || length == 1 || length == 2) return length;
int result = 1;
int duplicate;
int maxLine;
unordered_map<double, int> hashTable;
for (int i = 0; i < length; ++i) {
hashTable.clear();
duplicate = 0;
maxLine = 0;
for (int j = 0; j < length; ++j) {
if (points[i].x == points[j].x && points[i].y == points[j].y) ++duplicate;
else if (points[i].x == points[j].x && points[i].y != points[j].y) {
++hashTable[INT_MAX];
maxLine = max(maxLine, hashTable[INT_MAX]);
} else {
++hashTable[(double)(points[j].y-points[i].y)/(points[j].x-points[i].x)];
maxLine = max(maxLine, hashTable[(double)(points[j].y-points[i].y)/(points[j].x-points[i].x)]);
}
}
result = max(result, duplicate+maxLine);
}
return result;
}
};</span>
on a 2D plane, find the maximum number of points that lie on the same straight line.
Solution:
Code:
<span style="font-size:14px;">/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
class Solution {
public:
int maxPoints(vector<Point> &points) {
const int length = points.size();
if (length == 0 || length == 1 || length == 2) return length;
int result = 1;
int duplicate;
int maxLine;
unordered_map<double, int> hashTable;
for (int i = 0; i < length; ++i) {
hashTable.clear();
duplicate = 0;
maxLine = 0;
for (int j = 0; j < length; ++j) {
if (points[i].x == points[j].x && points[i].y == points[j].y) ++duplicate;
else if (points[i].x == points[j].x && points[i].y != points[j].y) {
++hashTable[INT_MAX];
maxLine = max(maxLine, hashTable[INT_MAX]);
} else {
++hashTable[(double)(points[j].y-points[i].y)/(points[j].x-points[i].x)];
maxLine = max(maxLine, hashTable[(double)(points[j].y-points[i].y)/(points[j].x-points[i].x)]);
}
}
result = max(result, duplicate+maxLine);
}
return result;
}
};</span>
相关文章推荐
- Java for LeetCode 149 Max Points on a Line
- LeetCode: Max Points on a Line [149]
- LeetCode Max Points on a Line
- LeetCode刷题笔录Max Points on a Line
- [LeetCode]Max Points on a Line
- LeetCode | Max Points on a Line
- Leetcode 149. Max Points on a Line
- Leetcode: Max Points on a Line
- [leetcode-149]Max Points on a Line(java)
- [LeetCode] 149. Max Points on a Line 共线点个数
- [LeetCode]149. Max Points on a Line 深入浅出讲解和代码示例
- Max Points on a Line(leetcode)
- 【leetcode】:max-points-on-a-line
- Leetcode:Max Points on a Line
- [LeetCode] Max Points on a Line
- LeetCode Max Points on a Line
- Leetcode Max Points on a Line
- leetcode - Max Points on a Line
- 【LeetCode-Hard-1】【Max Points on a Line】【点集中最大共线数】
- [Leetcode] 149. Max Points on a Line 解题报告