您的位置:首页 > 编程语言 > C语言/C++

LeetCode 149 — Max Points on a Line(C++ Java Python)

2014-03-02 10:50 841 查看
题目:http://oj.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.

题目翻译:

给定二维平面上的n个点,找出位于同一直线上的点的最大数目。
分析:

        以点为中心,计算斜率,用map(python为dictionary)保存斜率与次数的映射,注意点重合以及斜率为无穷的情况。

C++实现:

/**
* 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) {
int maxPoints = 0;
int curPoints = 0;
int samePoints = 0;
double slope = 0;
std::map<double, int> map;

for(int i = 0; i < points.size(); ++i)
{
curPoints = 1;
samePoints = 0;
map.clear();

for(int j = i + 1; j < points.size(); ++j)
{
if(points[j].x == points[i].x && points[j].y == points[i].y)
{
++samePoints;
}
else
{
if(points[j].x != points[i].x)
{
slope = 1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x);
}
else
{
slope = std::numeric_limits<double>::infinity();
}

if(map.find(slope) == map.end())
{
map[slope] = 2;
}
else
{
map[slope] += 1;
}

if(map[slope] > curPoints)
{
curPoints = map[slope];
}
}
}

curPoints += samePoints;

if(curPoints > maxPoints)
{
maxPoints = curPoints;
}
}

return maxPoints;
}
};

Java实现:

/**
* 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 maxPoints = 0;
int curPoints = 0;
int samePoint = 0;
double slope = 0;
Map<Double, Integer> map = new HashMap<Double, Integer>();

for (int i = 0; i < points.length; ++i) {
curPoints = 1;
samePoint = 0;
map.clear();

for (int j = i + 1; j < points.length; ++j) {
if (points[j].x == points[i].x && points[j].y == points[i].y) {
++samePoint;
} else {
if (points[j].x != points[i].x) {
slope = 1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x) + 0.0;  // to avoid -0.0
} else {
slope = Double.MAX_VALUE;
}

if (map.containsKey(slope)) {
map.put(slope, map.get(slope) + 1);
} else {
map.put(slope, 2);
}

if (map.get(slope) > curPoints) {
curPoints = map.get(slope);
}
}
}

curPoints += samePoint;

if (curPoints > maxPoints) {
maxPoints = curPoints;
}
}

return maxPoints;
}
}

Python实现:

# Definition for a point
# class Point:
#     def __init__(self, a=0, b=0):
#         self.x = a
#         self.y = b

class Solution:
# @param points, a list of Points
# @return an integer
def maxPoints(self, points):
if len(points) < 3:
return len(points)

maxPoints = 0

i = 0
while i < len(points):
curPoints = 1
samePoints = 0
dict = {}

j =  i + 1;
while j < len(points):
if points[j].x == points[i].x and points[j].y == points[i].y:
samePoints += 1
else:
if points[j].x != points[i].x:
slope = 1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x)
else:
slope = float('Inf')

if slope in dict:
dict[slope] += 1
else:
dict[slope] = 2

if dict[slope] > curPoints:
curPoints = dict[slope]

j += 1

curPoints += samePoints

if curPoints > maxPoints:
maxPoints = curPoints

i += 1

return maxPoints

        感谢阅读,欢迎评论!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode