直线上最多的点数

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

示例 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. 题解报告:poj 2823 Sliding Window(单调队列)

    Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...

  2. 转 pygame学习笔记(1)——安装及矩形、圆型画图

    http://www.cnblogs.com/xiaowuyi/archive/2012/06/06/2538921.html

  3. c#中stringbuilder的方法总结

    String 对象是不可改变的.每次使用 System.String 类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间.在需要对字符串执行重复修改的情况下,与创建新 ...

  4. log级别

    trace<debug<info<warn<error<fatal trace: 是追踪,就是程序推进以下,你就可以写个trace输出,所以trace应该会特别多,不过没 ...

  5. 将Jenkins的测试结果整合到Testlink

    如果试用Jenkins进行构建,构建中的测试结果可以直接作为Testlink的自动直接结果.   1. Testlink 中新增custom field.   2. 用例中custom field中加 ...

  6. Linux修改ssh端口,减少暴力破解

    版本centos7   注意:操作时请勿断开当前的ssh连接,以免发生情况登陆不了.   1.修改的是 /etc/ssh/sshd_config 文件 打开文件之后会发现Port是注释掉的,默认为22 ...

  7. 文档兼容性定义,使ie按指定的版本解析

    作为开发人员,特别是作为Web的前端开发人员 ,最悲催的莫过于要不断的,不断的去调试各种浏览器的显示效果,而这其中最让人头痛的莫过于MS下的IE系列浏览器,在IE系列中的调试我们将会发现没有一个是好伺 ...

  8. Common.Logging.dll----------配置方式,可选引用,用于日志输出

    1.简介common logging是一个通用日志接口,log4net是一个具体实现. common logging可以把输出连接到其他非log类上, 如EntLib的日志.NLog等 2.使用接下来 ...

  9. CREATE DATABASE - 创建新数据库

    SYNOPSIS CREATE DATABASE name [ [ WITH ] [ OWNER [=] dbowner ] [ LOCATION [=] 'dbpath' ] [ TEMPLATE ...

  10. hibernate 5.x版本中中schemaexport的使用

    public static void main(String[] args) { /*//创建hibernate配置对象 Configuration cfg = new Configuration() ...