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. ...
随机推荐
- jquery 保留两个小数的方法
$()); 直接使用:toFixed(2)
- 如何从maven资源库下载jar包
如何从maven资源库下载jar包 CreationTime--2018年6月7日09点00分 Author:Marydon 一.前提 需要安装并配置maven环境 二.准备工作 1.在桌面创建一 ...
- 【Docker】安装并测试安装成功
1.环境描述 Centos 7 2.安装步骤 通过命令yum install docker安装 等待下载安装-,出现下图,按y继续 继续等待-出现下图按y继续 再继续等待- 知道出现上图表示安装完毕 ...
- rabbitmq vhost
参考 http://blog.163.com/sky20081816@126/blog/static/16476102320107173226920/ http://blog.csdn.net/kev ...
- iOS-高仿微信摇一摇动画效果加震动音效
概述 摇一摇动画效果 (加震动音效) 详细 代码下载:http://www.demodashi.com/demo/10707.html 众所周知, 微信中的摇一摇功能: 搜索人/歌曲/电视,同样在一些 ...
- Xiuno 开发手册正式发布。
下载地址:http://bbs.xiuno.com/down/xiuno.chm.tar.gz
- virtualenv沙箱
准备用Django开发网站,豆瓣阅读中有<Django入门>,里面有人批注说用virtualenv+pip很好. 每次搞个什么都感觉举步维艰,因为之前太懒了,什么都懒得深入看一下.pip一 ...
- MySQL-五种日志(查询日志、慢查询日志、更新日志、二进制日志、错误日志)、备份及主从复制配置
开启查询日志: 配置文件my.cnf: log=/usr/local/mysql/var/log.log 开启慢查询: 配置文件my.cnf: log-slow-queries=/usr/local/ ...
- VC6.0设置注释快捷键
第一步:工具栏上右键选择Customize(定制),选择“Add-ins and Macro Files(附加项和宏文件)”页,把SAMPLE前面打上钩. 第二步:选择“Commands(命令)”页, ...
- ceph 之recovery machhine
一.概述 ceph recovery状态机的变化过程: