Submission Details

27 / 27 test cases passed.
Status:

Accepted

Runtime: 472 ms
Submitted: 0 minutes ago

Submitted Code

Language: java
  Edit Code  
 
 
 
 /**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
import java.util.HashMap;
import java.util.Map; public class Solution {
public int maxPoints(Point[] points) {
if (points == null || points.length == 0)
return 0;
int N = points.length;
if (N == 1)
return 1;
if (N == 2)
return 2; Map<String, Integer> dict = new HashMap<String, Integer>();
int maxOverall = 0;
for (int i = 0; i < N - 1; i++) {
int same = 1;
int max = 0;
for (int j = i + 1; j < N; j++) { int p1x = points[i].x;
int p1y = points[i].y;
int p2x = points[j].x;
int p2y = points[j].y; if (p1x == p2x && p1y == p2y) {
same++;
continue;
} int A = p2y - p1y;
int B = p1x - p2x; int GCD = gcd(A, B);
if (GCD != 0 && GCD != 1) {
A /= GCD;
B /= GCD;
}
if (A < 0) {
A = -A;
B = -B;
} String key = A + "," + B;
// System.out.println("round:" + i + " " + key);
int value = 1;
if (dict.containsKey(key)) {
value += dict.get(key);
}
dict.put(key, value);
max = max < value ? value : max;
} max += same;
maxOverall = maxOverall < max ? max : maxOverall;
dict.clear();
} return maxOverall;
} public int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
}
 
 
 
 

[LeetCode OJ] Max Points on a Line的更多相关文章

  1. [LeetCode OJ] 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.

    //定义二维平面上的点struct Point { int x; int y; Point(, ):x(a),y(b){} }; bool operator==(const Point& le ...

  2. 【leetcode】Max Points on a Line

    Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...

  3. [leetcode]149. 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. ...

  4. [LeetCode] 149. 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. ...

  5. 【leetcode】Max Points on a Line(hard)☆

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  6. Java for LeetCode 149 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. ...

  7. leetcode 149. Max Points on a Line --------- java

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  8. LeetCode之Max Points on a Line Total

    1.问题描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight ...

  9. leetcode[149]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. ...

随机推荐

  1. 2015.10.18 do while练习

    /*乘法表*/ #define COLMAX 10 #define ROWMAX 12 main() { int row,column,y; row=1; printf("          ...

  2. ad

    取消class和id前的元素限定 当你写给一个元素定义class或者id,你可以省略前面的元素限定,因为ID在一个页面里是唯一的,而clas s可以在页面中多次使用.你限定某个元素毫无意义.例如: d ...

  3. iOS开发~CocoaPods使用详细说明

    一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库. 二.安装 由于 ...

  4. Python循环语句

    1.Python循环类型 1.while循环:在某条件下,循环执行某段程序 a. while语句有两个重要命令:continue,break来跳出循环. continue用来跳出该次循环 break用 ...

  5. html画布

    一.<canvas>标签 Html5 引入了一个新的<canvas> 标签,这个标签所代表的区域就好象一块画布,你的所有图形绘制最后都要在这块画布上呈现.有了这个标签,浏览器的 ...

  6. PHP实例开发(3)PHP中MVC学习之ThinkPHP

    PHP中MVC学习之ThinkPHP 1.什么是MVC MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的是将M和V的实现代码分离 MVC是一个设 ...

  7. Dos学习笔记(1)dir命令

    这个命令是最常用的命令,就像linux的ls一样,同样他也有很多很多optionnal field供我们选择, 看了半天,觉得自己离盲打肯定还是有很大的差距的,现在只是想体验一下dos,或者说感受下这 ...

  8. C的文件操作2

    [转] C语言文件操作  概述 所谓文件(file)一般指存储在外部介质上数据的集合,比如我们经常使用的mp3.mp4.txt.bmp.jpg.exe.rmvb等等.这些文件各有各的用途,我们通常将它 ...

  9. jquery与各种UI框架的导入要注意的地方

    前端的处理我们会使用easyUI,amazeUI,bootstrap等等框架,然而每个页面都要导入这些js   css 文件,所以我们将要导入的文件提取出来,写在一个页面上,每次只要倒入该页面就行,如 ...

  10. redis学习

    wget http://labfile.oss.aliyuncs.com/files0422/redis-2.8.9.tar.gz .tar.gz cd redis- make make instal ...