直线上最多的点数

给定一个二维平面,平面上有 个点,求最多有多少个点在同一条直线上。

示例 1:

输入: [[1,1],[2,2],[3,3]]

输出: 3

解释:

^

|

|        o

|     o

|  o

+------------->

0  1  2  3 4

示例 2:

输入: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]

输出: 4

解释:

^

|

| o

|     o   o

|      o

|  o   o

+------------------->

0  1  2  3  4  5  6

 class Solution {
public int maxPoints(Point[] points) {
if(points.length == 0) {
return 0;
}
int[] count = new int[points.length];
for (int i = 0; i < points.length; i++) {
count[i] = 1;
int size = 1;
int same = 0;
HashMap<Integer[], Integer> hashMap = new HashMap<>();
for (int j = 0; j < points.length; j++) {
if(i != j) {
if(points[i].x != points[j].x) {
int dy = points[i].y - points[j].y;
int dx = points[i].x - points[j].x;
int gcd = generateGCD(dy, dx);
if(gcd != 0) {
dy = dy / gcd;
dx = dx / gcd;
}
Integer[] nums = new Integer[2];
nums[0] = dy;
nums[1] = dx;
boolean flag = false;
for (Integer[] array : hashMap.keySet()) {
if(nums[0] == array[0] && nums[1] == array[1]) {
flag = true;
hashMap.put(array, hashMap.get(array) + 1);
}
}
if(!flag) {
hashMap.put(nums, 1);
}
}else {
if(points[i].y == points[j].y) {
same++;
}
size++;
}
}
}
for (Integer[] array : hashMap.keySet()) {
if(hashMap.get(array) + 1 > count[i]) {
count[i] = hashMap.get(array) + 1;
}
}
count[i] += same;
count[i] = Math.max(count[i], size);
}
int maxIndex = 0;
for (int i = 1; i < count.length; i++) {
if(count[i] > count[maxIndex]) {
maxIndex = i;
}
}
return count[maxIndex];
} // 欧几里得算法:计算最大公约数
private int generateGCD(int x, int y) {
if (y == 0)
return x;
return generateGCD(y, x % y);
}
}

Leetcode 149.直线上最多的点数的更多相关文章

  1. Java实现 LeetCode 149 直线上最多的点数

    149. 直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | ...

  2. leetcode 149. 直线上最多的点数 解题报告

    给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | o +------- ...

  3. 149 Max Points on a Line 直线上最多的点数

    给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...

  4. [Swift]LeetCode149. 直线上最多的点数 | 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. Max Points on a Line(直线上最多的点数)

    给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | |        o |     o | ...

  6. POJ 1118 Lining Up 直线穿过最多的点数

    http://poj.org/problem?id=1118 直接枚举O(n^3) 1500ms能过...数据太水了...这个代码就不贴了... 斜率排序O(n^2logn)是更好的做法...枚举斜率 ...

  7. lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上

    题目 最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...

  8. LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard

    题目:Max Points on a line Given n points on a 2D plane, find the maximum number of points that lie on ...

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

随机推荐

  1. Person p = new Person("zhangsan",20);该句话都做了什么事情?

    1,因为new用到了Person.class.所以会先找到Person.class文件并加载到内存中.2,执行该类中的static代码块,如果有的话,给Person.class类进行初始化.3,在堆内 ...

  2. B/S和C/S示意图

    B/S C/S

  3. jmeter(六)关联

    话说LoadRunner有的一些功能,比如:参数化.检查点.集合点.关联,Jmeter也都有这些功能,只是功能可能稍弱一些,今天就关联来讲解一下. JMeter的关联方法有两种:后置处理器-正则表达式 ...

  4. python_one-day

    python入门_(1) 作者:_晓冬 归档:学习笔记 2017/9/9 目  录 第1章 练习... 1 1.1 格式化输出... 1 1.2 流程控制if..else. 1 1.3 流程控制whi ...

  5. Swift 基础语法入门(一)

    一.变量和常量 1.声明常量和变量 用let来声明常量  let radius = 10 用var来声明变量 var age = 20   或者是var x = 0.0, y = 0.0, z = 0 ...

  6. 学习笔记 第八章 使用CSS美化列表

    第8章  使用CSS美化列表 8.1 列表的基本结构 在HTML中,列表结构可以分为两种基本类型:有序列表和无序列表.使用标签如下: <ul>...</ul>:标识无序列表: ...

  7. 在Windows7下编译调试C#程序

    要在 命令行下编译C#代码,要配置一下 1.在环境变量下新建一个变量 参数名: csc 参数值:C:\Windows\Microsoft.NET\Framework\v4.0.30319 2.在系统变 ...

  8. Android学习笔记(六) Activity和View基础

    一.Activity的启动流程 操作系统首先查看AndroidManifest.xml来决定启动哪一个Activity. 生成对应的Activity对象(系统自动完成). 调用它的onCreate() ...

  9. Javaweb项目构建常见问题

    eclipse中 将java项目转换为web项目 1.找到项目工作空间目录,打开.project文件,并修改文件, 修改如下:      找到:<natures> </natures ...

  10. java web 学习笔记 - 表达式语言

    1.表达式语言简介 主要为了简化mvc中 jsp的代码量,方便进行属性的输出.还可以避免进行属性为空等的判断,表达式默认将null设置为"". 表达式语言的一个最大的好处就是,只需 ...