[LeetCode OJ] Max Points on a Line
Submission Details
|
27 / 27 test cases passed.
|
Status:
Accepted |
|
Runtime: 472 ms
|
Submitted: 0 minutes ago
|
Submitted 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的更多相关文章
- [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 ...
- 【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 ...
- [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. ...
- [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. ...
- 【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. ...
- 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. ...
- 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. ...
- 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 ...
- 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. ...
随机推荐
- RadioButtonList的使用
前台绑定: <asp:RadioButtonList ID="hlBatchYuJi" runat="server" RepeatColumns=&quo ...
- 观 GT Java语言管理系统的感悟
继上次java系统考核完... 坦白说,我对我自己写的例子还是很满意的,虽说学长们给的评价不高 ,但我一直以为是学长们对我们的要求太高,以他们的眼光在看待我们,所以我对学长们给的评价并没有太过在意,当 ...
- 什么是Ajax
AJAX不是一种新的编程语言,而是一种用于创建更好更快以及交互性更强的Web应用程序的技术. 使用Javascript向服务器提出请求并处理响应而不阻塞用户!核心对象XMLHTTPRequest.通过 ...
- Switch能否用string做参数?
由于小编自己的编译环境jdk用的是比较高,在命令窗口输入java -version即可看到.(打开命令窗口win+R,输入cmd,回车) java version "1.8.0_111&qu ...
- php 屏蔽NOTICE报错机制代码
error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT^E_NOTICE
- linux下一些可用库
1. musl: 为了夸平台,采用小巧玲珑的libc进行静态编译.
- B-index、bitmap-index、text-index使用场景详解
索引的种类:B-tree索引.Bitmap索引.TEXT index1. B-tree索引介绍: B-tree 是一种常见的数据结构,也称多路搜索树,并不是二叉树.B-tree 结构可以显著减少定位 ...
- python小知识点
问题:求列表中每个元素的元素次方之和>>> a=[1,2,3,4]>>> k=len(a)第一种解法# s=0# for x in a:# ...
- 获取字符串中img标签的url集合(转载)
/// <summary> /// 获取字符串中img的url集合 /// </summary> /// <param name="content"& ...
- 转 MySQL 数据备份与还原
MySQL 数据备份与还原 原贴:http://www.cnblogs.com/kissdodog/p/4174421.html 一.数据备份 1.使用mysqldump命令备份 mysqldum ...