[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. ...
随机推荐
- HTML5,jQuery,ajax基础面试
简要描述HTML5中的本地存储 答案: 很多时候我们会存储用户本地信息到电脑上,例如:比方说用户有一个填充了一半的长表格,然后突然网络连接断开了,这样用户希望你能存储这些信息到本地,当网络回复的时候, ...
- windows下使用Git命令汇总
这里只是简单汇总下Git主要命令,方便记忆:汇总的不好,请各位包容,谢谢!想看详细讲解,推荐廖雪峰大神的教程,地址如下:http://www.liaoxuefeng.com/wiki/00137395 ...
- C++的深拷贝与浅拷贝
对于普通类型的对象来说,它们之间的复制是很简单的,例如:int a=88;int b=a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量.下面是一个类对象拷贝的简单例子. #i ...
- OS实验报告--FCFS算法
实验二.作业调度模拟实验 专业:商业软件工程 姓名:王泽锴 学号:201406114113 一.实验目的 (1)加深对作业调度算法的理解: (2)进行程序设计的训练. 二.实验内容和要求 (1)实验 ...
- 给linux 授权一个可以远程登录的账户
创建用户:grant all on *.* to '; 授权:flush privileges;
- 三级联动(在YII框架中)
//三级联动 //数据库代码过多就不上传了 //视图 <div class="area"> <table class="table"&g ...
- LEETCODE —— Unique Binary Search Trees [动态规划]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Shiro中的subject.login()
当调用ShiroHandler中的subject.login()的时候,会自动调用Realm中的doGetAuthenticationInfo方法.
- WIFI基本知识整理
这里对wifi的802.11协议中比较常见的知识做一个基本的总结和整理,便于后续的学习.因为无线网络中涉及术语很多,并且许多协议都是用英文描述,所以有些地方翻译出来会有歧义,这种情况就直接英文来描述了 ...
- 知识积累:CA详解
所有证书有多种文件编码格式,主要包括: CER编码(规范编码格式):是BER(基本编码格式)的一个变种,比BER规定得更严格DER编码(卓越编码格式):是BER(基本编码格式)的一个变种, 比BER ...