iNVAiN

博客园 首页 新随笔 联系 订阅 管理

Submission Details

27 / 27 test cases passed.
Status: 

Accepted

Runtime: 472 ms
Submitted: 0 minutes ago

Submitted Code

Language: java
 
 
 
 1 /**
 2  * Definition for a point.
 3  * class 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 import java.util.HashMap;
11 import java.util.Map;
12  
13  
14 public class Solution {
15     public int maxPoints(Point[] points) {
16         if (points == null || points.length == 0)
17             return 0;
18         int N = points.length;
19         if (N == 1)
20             return 1;
21         if (N == 2)
22             return 2;
23  
24         Map<String, Integer> dict = new HashMap<String, Integer>();
25         int maxOverall = 0;
26         for (int i = 0; i < N - 1; i++) {
27             int same = 1;
28             int max = 0;
29             for (int j = i + 1; j < N; j++) {
30  
31                 int p1x = points[i].x;
32                 int p1y = points[i].y;
33                 int p2x = points[j].x;
34                 int p2y = points[j].y;
35  
36                 if (p1x == p2x && p1y == p2y) {
37                     same++;
38                     continue;
39                 }
40  
41                 int A = p2y - p1y;
42                 int B = p1x - p2x;
43  
44                 int GCD = gcd(A, B);
45                 if (GCD != 0 && GCD != 1) {
46                     A /= GCD;
47                     B /= GCD;
48                 }
49                 if (A < 0) {
50                     A = -A;
51                     B = -B;
52                 }
53  
54                 String key = A + "," + B;
55 //              System.out.println("round:" + i + " " + key);
56                 int value = 1;
57                 if (dict.containsKey(key)) {
58                     value += dict.get(key);
59                 } 
60                 dict.put(key, value);
61                 max = max < value  ? value  : max;
62             }
63             
64             max += same;
65             maxOverall = maxOverall < max ? max : maxOverall;
66             dict.clear();
67         }
68  
69         return maxOverall;
70     }
71  
72     public int gcd(int a, int b) {
73         if (b == 0)
74             return a;
75         return gcd(b, a % b);
76     }
77 }
78  

 

 
 
 
 
posted on 2014-05-01 20:04  iNVAiN  阅读(255)  评论(0编辑  收藏  举报