[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. ...
随机推荐
- DEDECMS 留言薄模块的使用方法
一.留言薄的安装 留言薄的安装过程和其他插件一样,首先我们进入后台模块管理列表,点击其对应的“安装”: 以上步骤,我们完成了留言薄插件的安装. 二.留言薄的卸载 留言薄的卸载,同样首先我们要进入模块管 ...
- web安全之sqlload_file()和into outfile()
load_file() 条件:要有file_priv权限 知道文件的绝对路径 能使用union 对web目录有读权限 如果过滤啦单引号,则可以将函数中的字符进行hex编码 步骤: 1.读/etc/in ...
- 输入输出流(IO)
输入输出流(IO)文件(File)java.io.File用于表示文件(目录),也就是说程序员可以通过File类在程序中操作硬盘上的文件和目录.File类只用于表示文件(目录)的信息(名称.大小等), ...
- 第三篇T语言实例开发,图色操作
---恢复内容开始--- 图色的基本操作 1.找颜色色命令的基本操作 坐标点取色:获取指定坐标点的颜色 区域找色:在指定区域里找某一个颜色 模糊找色:在指定区域里找某一个颜色,可以设置相似度 多点找色 ...
- nodejs-2:模块与包管理工具
多人协作时,大量的js文件批量的引入到页面中,会出现变量被覆 盖掉方法被重写掉的情况,特别是存在一些依赖关系的时候,还容 易导致页面出错,这是因为js天生就缺少一种模块的管理机制来 隔离实现功能的js ...
- 【WEB前端】使用百度ECharts,绘制项目质量报表
一.下载ECharts的js库 下载地址:http://echarts.baidu.com/download.html 由于我们对体积无要求,所以我们采用了完整版本,功能齐全,在项目中,我们只需要像普 ...
- multithreading - Reader/Writer Locks in C++
You Only Need To Note This: only 1 single thread can acquire an upgrade_lock at one time. others are ...
- Atom编辑器添加eclipse快捷键
Settings - Keybindings - 点击"your keymap file" 'atom-text-editor': 'alt-/': 'autocomplete- ...
- 使用snmp+mrtg监控CPU、流量、磁盘空间、内存
1.安装snmp rpm -qa|grep snmp* //查看是否安装了snmpyum -y install snmp* //安装snmp #vim /etc/snmp/snmpd.confroco ...
- Java中引用类型变量,对象,值类型,值传递,引用传递 区别与定义
一.Java中什么叫做引用类型变量?引用:就是按内存地址查询 比如:String s = new String();这个其实是在栈内存里分配一块内存空间为s,在堆内存里new了一个Stri ...