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 on the same straight line.

SOLUTION 1:
全部的点扫一次,然后计算每一个点与其它点之间的斜率。
创建一个MAP, KEY-VALUE是 斜率:线上的点的数目。
另外,注意重合的点,每次都要累加到每一条线上。
注意:
1. k = 0 + (double)(points[i].y - points[j].y)/(double)(points[i].x - points[j].x);
使用这个公式来计算的原因是 (double)(points[i].y - points[j].y)/(double)(points[i].x - points[j].x) 有可能计算出0和-0
0+(-0)后,就会都变成0.
2. 用Dup来计算重合的点。
3. 如果某点开始所有的点都在一起,则至少Max = Math.max(max, duplicate)。
4. 注意每次换一个点计算时,map要重建。因为即使K相同,只代表线是平等,不代表会是同一条线。
public class Solution {
public int maxPoints(Point[] points) {
int max = ;
if (points == null) {
return ;
}
int len = points.length;
for (int i = ; i < len; i++) {
// Create a map to recode all the numbers of elements of every K.
HashMap<Double, Integer> map = new HashMap<Double, Integer>();
// ItSelf.
int dup = ;
for (int j = i; j < len; j++) {
// the same point.
if (points[i].x == points[j].x && points[i].y == points[j].y) {
dup++;
continue;
}
double k = Double.MAX_VALUE;
if (points[i].x != points[j].x) {
k = + (double)(points[i].y - points[j].y)/(double)(points[i].x - points[j].x);
}
if (map.containsKey(k)) {
map.put(k, map.get(k) + );
} else {
map.put(k, );
}
}
max = Math.max(max, dup);
for (int n: map.values()) {
max = Math.max(max, n + dup);
}
}
return max;
}
}
主页君的GitHub:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/hash/MaxPoints.java
参考答案:
http://www.ninechapter.com/solutions/max-points-on-a-line/
LeetCode: Max Points on a Line 解题报告的更多相关文章
- 【LeetCode】149. Max Points on a Line 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...
- [leetcode]Max Points on a Line @ Python
原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/ 题意:Given n points on a 2D plane, find th ...
- [LeetCode] 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 题解
题意 Given n points on a 2D plane, find the maximum number of points that lie on the same straight lin ...
- LeetCode:Max Points on a Line
题目链接 Given n points on a 2D plane, find the maximum number of points that lie on the same straight l ...
- 【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 OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- 【LeetCode】149. 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 on the ...
- [LintCode] 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. ...
随机推荐
- Android中onTouch与onClick事件的关系
这几天遇到点关于Android的触摸事件相关的,还跟onClick有关.暂且记下: LinearLayout分别设置了onTouchListener,onClickListener,onLongCli ...
- 〖Linux〗Qt+gsoap开发客户端程序,服务端地址设定的字符串转换处理
之所以写出来,是由于经常因为这个问题屡屡丢面子.. 一般情况下,QString转换成(char*),我们一般直接使用: char *str = qstr->text().toLatin1().d ...
- SDUT 1157-小鼠迷宫问题(BFS&DFS)
小鼠迷宫问题 nid=24#time" title="C.C++.go.haskell.lua.pascal Time Limit1500ms Memory Limit 65536 ...
- ValueError: Expecting property name: line 1 column 1 (char 1)
# -*- coding: cp936 -*- #xiaodeng #python 2.7.10 import weibo s='{"name":"xiaodeng&qu ...
- php-fpm 日志
1.php-fpm 错误日志 #默认位置 安装目录下的 log/php-fpm.log error_log = log/php-fpm.log #错误级别 alert(必须立即处理), error(错 ...
- Java中初级数值类型的大小, volatile和包装类wrapped type的比较
Java中的初级数值类型 Java是静态类型语言, 所有的变量必须先声明再使用. 其初级类型一共8种: boolean: 数据只包含1bit信息, 但是占空间为8-bit, 默认值为false byt ...
- spring thymeleaf 自定义标签
概述 thymeleaf2.1.5自定义标签及自定义属性案例,类似于JSP中的自定义JSTL标签 详细 代码下载:http://www.demodashi.com/demo/10495.html 一. ...
- PHP-Open Flash Chart学习一(swfobject知识)
首先必须了解下swfobject的知识 在网页里面插入swf再平常不过了, 一般会想到如下代码: <object classid="clsid:D27CDB6E-AE6D-11cf-9 ...
- POJ 3691 DNA repair (DP+AC自动机)
DNA repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4815 Accepted: 2237 Descri ...
- Kafka日志清除策略
一.更改日志输出级别 config/log4j.properties中日志的级别设置的是TRACE,在长时间运行过程中产生的日志大小吓人,所以如果没有特殊需求,强烈建议将其更改成INFO级别.具体修改 ...