Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

 求二维平面上n个点中,最多共线的点数。
 
 
1、比较直观的方法是,三层循环,以任意两点划线,判断第三个点是否在这条直线上。
 
比较暴力
 
2、使用map来记录每个点的最大数目。
 
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
public int maxPoints(Point[] points) {
if (points.length < 3) return points.length; int max = 0;//用于返回的结果,即共线点的最大个数
Map<Double, Integer> map = new HashMap<Double, Integer>();//保存同一个斜率的点的个数 for (int i = 0; i < points.length; i++) {//以每一个点为固定点
map.clear();
int duplicate = 1;//记录跟固定点重合的个数 for(int j = i+1 ; j < points.length; j++){//遍历其他点,求其与固定点之间的斜率
double slope = 0.0;//斜率 if (points[i].x == points[j].x && points[i].y == points[j].y) {//如果跟固定点重合
duplicate++;
continue;
} else if (points[i].x == points[j].x) {//如果跟固定点在同一条竖线上,斜率设为最大值
slope = Integer.MAX_VALUE;
} else if( points[i].y == points[j].y ){//计算该点与固定点的斜率
slope = 0;
} else{
slope = 1.0 * (points[i].y - points[j].y) / (points[i].x - points[j].x);
}
map.put(slope, map.containsKey(slope) ? map.get(slope) + 1 : 1);
} //更新最优解
if (map.keySet().size() == 0) {//如果map为空
max = duplicate > max ? duplicate : max;
} else {
for (double key : map.keySet()) {
max = Math.max((duplicate + map.get(key)), max);
}
}
}
return max;
}
}

leetcode 149. Max Points on a Line --------- java的更多相关文章

  1. [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. ...

  2. 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. ...

  3. [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. ...

  4. 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. ...

  5. 【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 ...

  6. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  7. 【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 ...

  8. 【LeetCode】149. Max Points on a Line 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...

  9. 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 ...

随机推荐

  1. 数据结构-List

    Lis的实现: /////////////////////////////////////////////////////////////////////////////// // // FileNa ...

  2. CSS 水平居中

    一.水平居中:行内元素解决方案 居中元素:文字.链接以及其它行内元素(inline或inline-*类型的元素,如inline-block,inline-table,inline-flex)解决方案: ...

  3. Android 系统基础

    当系统启动一个组件,它其实就启动了这个程序的进程(如果这个进程还未被启动的话)并实例化这个组件所需要的类. 例如,如果你的程序启动了相机程序里的activity去拍照,这个activity实际上是运行 ...

  4. JS小问题总结

    1. 超链接中href=#与href=javascript:void(0) 的区别  #包含了一个位置信息.默认的锚是#top 也就是网页的上端:而javascript:void(0)   仅仅表示一 ...

  5. julia与python中的列表解析.jl

    julia与python中的列表解析.jl #=julia与python中的列表解析.jl 2016年3月16日 07:30:47 codegay julia是一门很年轻的科学计算语言 julia文档 ...

  6. linux学习笔记3:linux的网络配置,rpm包,shell以及samba服务器的使用和安装

    1.linux下的shell<linux命令.编辑器和shell编程> (1)shell种类有很多,常用的有三种,在linux可以通过ls -l /bin/*sh 来显示所有已安装的she ...

  7. lib静态链接库,dll动态链接库,h文件

    最近在弄摄像头,发现我在调用摄像头自带的函数的时候,库没连接上,于是经过高人指点,学习了一下lib静态链接库,dll动态链接库来补充一下自己的基础知识. 一.首先我们来介绍一下lib静态链接库. li ...

  8. 毕向东day23--java基础-网络总结

    传输层:TCP/UDP   UDP例如:qq聊天,录屏软件,桌面共享     TCP建立链接:三次握手,例如,我叫你一声老王(一次),老王回答说:到.(二次),我对老王说,我知道你到了.(三次握手)! ...

  9. 自定义的BroadCastReceiver

    1.MainActivity2.java中的代码,主要是使用意图发送广播 public class MainActivity2 extends Activity{ @Override protecte ...

  10. 2016-1-9 Quartz框架的学习,剪裁图片并设置边框

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...