码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode – Refresh – Max Points on a Line

时间:2015-03-20 09:17:17      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:

Notes:

1. Do not forget to check xi == xj. It will cause INF.

2. DO not forget to initialize the iterator.

 1 /**
 2  * Definition for a point.
 3  * struct Point {
 4  *     int x;
 5  *     int y;
 6  *     Point() : x(0), y(0) {}
 7  *     Point(int a, int b) : x(a), y(b) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int maxPoints(vector<Point> &points) {
13         int len = points.size(), duplicates = 1, result = 0;
14         if (len < 3) return len;
15         unordered_map<double, int> mapping;
16         for (int i = 0; i < points.size(); i++) {
17             mapping.clear();
18             mapping[INT_MIN] = 0;
19             duplicates = 1;
20             for (int j = 0; j < len; j++) {
21                 if (i == j) continue;
22                 if (points[i].x == points[j].x && points[i].y == points[j].y) {
23                     duplicates++;
24                     continue;
25                 }
26                 if (points[i].x == points[j].x) {
27                     mapping[INT_MAX]++;
28                     continue;
29                 }
30                 double k = double(points[i].y - points[j].y)/(points[i].x - points[j].x);
31                 mapping[k]++;
32             }
33             for (unordered_map<double, int>::iterator it = mapping.begin(); it != mapping.end(); it++) {
34                 result = max(result, it->second + duplicates);
35             }
36         }
37         return result;
38     }
39 };

 

LeetCode – Refresh – Max Points on a Line

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4352773.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!