Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
2018-02-19 12:13
543 查看
题目:Given n points on a 2D plane, find the maximum number
of points that lie on the same straight line.(给的点是用vector保
存)/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/思路:
构建双重for循环,相当于起始点与终点
一个起始点和斜率可以确定一条直线,用map<double, int>来保存对应的斜率与该直线上出现的点数的值
三种情况:重合点(用dup来保存),斜率不存在的垂直点(用vertical来保存),斜率可算的正常情况class Solution {
public:
int maxPoints(vector<Point> &points) {
int size = points.size();
if(size < 0)return -1;
if(size == 0)return 0;
if(size == 1)return 1;
map<double, int> mymap;
int res = 0; //the final result
for(int i = 0; i < size; ++i){
int vertical = 0; //垂直点
int dup = 0; //重合点duplicate
int curmax = 1;
for(int j = 0; j < size; ++j){
if(i != j){
double delt_x = points[i].x - points[j].x; //delt_* must be declared double
double delt_y = points[i].y - points[j].y;
if((int)delt_y == 0 && (int)delt_x == 0){ //重合点
dup++;
}else if(delt_x == 0){ //垂直点
if(vertical == 0)vertical = 2;
else vertical++;
curmax = max(vertical, curmax);
}else{ //common situation
double k = delt_y / delt_x;
if(mymap[k] == 0)mymap[k] = 2;
else mymap[k]++;
curmax = max(mymap[k], curmax);
}
}
}
res = max(res, curmax+dup);
}
return res;
}
};
of points that lie on the same straight line.(给的点是用vector保
存)/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/思路:
构建双重for循环,相当于起始点与终点
一个起始点和斜率可以确定一条直线,用map<double, int>来保存对应的斜率与该直线上出现的点数的值
三种情况:重合点(用dup来保存),斜率不存在的垂直点(用vertical来保存),斜率可算的正常情况class Solution {
public:
int maxPoints(vector<Point> &points) {
int size = points.size();
if(size < 0)return -1;
if(size == 0)return 0;
if(size == 1)return 1;
map<double, int> mymap;
int res = 0; //the final result
for(int i = 0; i < size; ++i){
int vertical = 0; //垂直点
int dup = 0; //重合点duplicate
int curmax = 1;
for(int j = 0; j < size; ++j){
if(i != j){
double delt_x = points[i].x - points[j].x; //delt_* must be declared double
double delt_y = points[i].y - points[j].y;
if((int)delt_y == 0 && (int)delt_x == 0){ //重合点
dup++;
}else if(delt_x == 0){ //垂直点
if(vertical == 0)vertical = 2;
else vertical++;
curmax = max(vertical, curmax);
}else{ //common situation
double k = delt_y / delt_x;
if(mymap[k] == 0)mymap[k] = 2;
else mymap[k]++;
curmax = max(mymap[k], curmax);
}
}
}
res = max(res, curmax+dup);
}
return res;
}
};
相关文章推荐
- Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
- 题目描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight
- Leetcode--->Given n points on a 2D plane, find the maximum number of points that lie on the same str
- [LeetCode]Maximum number of points on a straight line in 2d plane
- 数组空间Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- [CC150] Find a line passing the most number of points
- [CareerCup] 7.6 The Line Passes the Most Number of Points 经过最多点的直线
- VMware排错:Number of virtual devices exceeds the maximum for a given controller
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
- jxl.write.biff.RowsExceededException: The maximum number of rows permitted on a worksheet been excee
- Given an array of size n, find the majority element. The majority element is the element that appear
- #736 – 通过代码获取硬件支持的最大触摸点数(Finding the Maximum Number of Touch Points at Run-time)
- Count the number of bits that are on in an unsigned integer(计算一个无符整数中1Bit的个数)-- (1)
- Maximum number of things to show on the main page"
- CareerCup Find the smallest range that includes at least one number from each of the k sorted lists.
- from given array of n elements find the maximum element for each consecutive sub-array of k elements.
- Data Structure Array: Given an array arr[], find the maximum j – i such that arr[j] > arr[i]
- given an array of integers Find the index of values that satisfy A+B = C + D
- Write a method that counts the number of occurrences of a given character in a string