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.
解题思路:
本题主要需要考虑到斜线的情况,可以分别计算出过points[i]直线最多含几个点,然后算出最大即可,由于计算points[i]的时候,前面的点都计算过了,所以不需要把前面的点考虑进去,所以问题可以转化为过points[i]的直线最大点的个数,解题思路是用一个HashMap储存斜率,遍历points[i]后面每个点,看看和points[i]的斜率是否出现过,这里有个问题,如何储存斜率,理想状况下,斜率应该用一个String表示,如(0,0)和(2,4)可以存储为"1k2"这样的类型,这会涉及到求最大公约数的问题,实现起来比较麻烦,实际上直接用double表示即可通过,这是因为((double)1/((double)Integer.MAX_VALUE-(double)Integer.MIN_VALUE))是能输出2.3283064370807974E-10的。因此,我们直接用double即可。
JAVA实现如下:
public int maxPoints(Point[] points) {
if (points.length <= 2)
return points.length;
int max = 2;
for (int i = 0; i < points.length; i++) {
int pointMax = 1, samePointCount = 0;
HashMap<Double, Integer> slopeCount = new HashMap<Double, Integer>();
Point origin = points[i];
for (int j = i + 1; j < points.length; j++) {
Point target = points[j];
if (origin.x == target.x && origin.y == target.y) {
samePointCount++;
continue;
}
double k;
if (origin.x == target.x)
k = Float.POSITIVE_INFINITY;
else if (origin.y == target.y)
k = 0;
else
k = ((double) (origin.y - target.y))
/ (double) (origin.x - target.x);
if (slopeCount.containsKey(k))
slopeCount.put(k, slopeCount.get(k) + 1);
else
slopeCount.put(k, 2);
pointMax = Math.max(pointMax, slopeCount.get(k));
}
max = Math.max(max, pointMax + samePointCount);
}
return max;
}
Java for LeetCode 149 Max Points on a 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 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] 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】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 ...
- [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】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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...
- 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 li ...
随机推荐
- 用php生成数据字典
<?php header("Content-type: text/html; charset=utf-8"); $dbserver = "localhost&quo ...
- PowerDesigner逆向工程从现有数据库生成PDM
如题,我想对于一个旧系统或者帮别人的系统进行擦屁股时,数据库设计以及关系都是非常好的切入点: 使用这个方法的前提,就是在数据库设计中,已经有明确的主外键关系(这里只针对中小型设计,业务逻辑强的,对于特 ...
- android studio中the logging tag can be most 23 characters
转:http://blog.csdn.net/voiceofnet/article/details/49866047 今天写代码的时候,突然发现平时用的好好的Log竟然报错,提示信息为:the log ...
- POJ3041Asteroids(最小点覆盖+有点小抽象)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18289 Accepted: 9968 Descri ...
- Nagios告警和监控主机安装介绍(三)
Nagios邮件告警 配置sendEmail 解压缩tar –zxvf sendEmail-v1.56.tar.gz cd sendEmail-v1.56 将可执行程序复制cp sendEmail / ...
- view视图文件中的input等输入框必须含有name属性,不然控制器里的动作formCollection是没有值的
view视图文件中的input等输入框必须含有name属性,不然控制器里的动作formCollection是没有值的,就是没有name属性,后台获取不到值
- Servlet------(声明式)异常处理
Test.java 其他方法不变,重写 protected void service()方法 public void init(ServletConfig config) throws Servlet ...
- 高性能的分布式内存对象缓存系统Memcached
Memcached概述 什么是Memcached? 先看看下面几个概念: Memory:内存存储,不言而喻,速度快,对于内存的要求高,不指出的话所缓存的内容非持久化.对于CPU要求很低,所以常常采 ...
- word双面打印的方法
首先要明白打印的过程 先进入打印机的纸张顶部,肯定是先打印,这样就确定了纸张放置的方向,不会放倒了 肯定是打印在纸张的向上的那一面,这样就确定了打印的正反面 纸张打印的顺序肯定是从上到下,这样就确定了 ...
- c/c++指针
指针主要分: 指向单一的某个对象/变量, 用于多态或函数指针, 这个不难 - 一级指针 其次是指向数组, 用来操作/遍历数组元素 - 一级/二级指针 指向数组的一级指针很简单了: p指向的是数组的元素 ...