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 };

 

posted on 2015-03-20 09:00  keepshuatishuati  阅读(154)  评论(0编辑  收藏  举报