[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. ...
随机推荐
- 【转】rollup、cub、grouping sets、grouping、grouping_id在报表中的应用
摘自 http://blog.itpub.net/26977915/viewspace-734114/ 在报表语句中经常要使用各种分组汇总,rollup和cube就是常用的分组汇总方式. 第一:gro ...
- iOS 自定义选项卡-CYLTabBarController
正常的选项卡流程 cocoapods就不说了 创建一个CYLTabBarControllerConfig类 #import <Foundation/Foundation.h> #impor ...
- 【Cocos2d-x 3.x】 场景切换生命周期、背景音乐播放和场景切换原理与源码分析
大部分游戏里有很多个场景,场景之间需要切换,有时候切换的时候会进行背景音乐的播放和停止,因此对这块内容进行了总结. 场景切换生命周期 场景切换用到的函数: bool Setting::init() { ...
- 2016.12.01 搭建dendroid备忘
在2014年的时候看了freebuf的那篇,感觉很6,2014年搭了就一遍成功了,事过两年,物是人非啊,2016搞了云,没事测试,搞了一遍死活不成功,第二天测试成功,过程逗比坎坷,没什么难的 //环境 ...
- UVa 12118 检查员的难题(dfs+欧拉回路)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- URL Scheme APP跳转safari以及跳回APP
上图 : 在plist文件里面设置. URL identifier 一般为反域名+项目名称 (尽可能保证少重复) URL Schemes是一个数组.一个APP可以添加多个.该参数为跳转时使用的标识. ...
- 【引】objective-c,4:category的原理
objc_category主要参考博文: http://blog.leichunfeng.com/blog/2015/05/18/objective-c-category-implementation ...
- makefile编写要点
1.wildcard : 扩展通配符 2.notdir : 去除路径 3.patsubst :替换通配符 格式:$(patsubst <pattern>,<replacement&g ...
- 手机web——自适应网页设计(html/css控制)
一. 允许网页宽度自动调整: "自适应网页设计"到底是怎么做到的? 其实并不难. 首先,在网页代码的头部,加入一行viewport元标签. <meta name=" ...
- WPF-流文档元素
1.Block元素 用于分组其他元素 Paragraph是块级别元素,文本段落 Paragraph.Inlines集合内. 设置第一行缩进量Paragraph.TextIndet 2.Inline元素 ...