直线上最多的点数

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

示例 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. [Usaco2006 Jan] Dollar Dayz 奶牛商店

    Description 约翰到奶牛商场里买工具.商场里有K(1≤K≤100).种工具,价格分别为1,2,-,K美元.约翰手里有N(1≤N≤1000)美元,必须花完.那他有多少种购买的组合呢? Inpu ...

  2. jmeter(一)工具介绍(二)

    1.Jmeter 概要描叙 jmeter 是一款专门用于功能测试和压力测试的轻量级测试开发平台.多数情况下是用作压力测试,该测试工具在阿里巴巴有着广泛的使用,估计是不要钱吧,哈哈,功能上来说,整个平台 ...

  3. [转]MySQL游标的使用

    转自:http://www.cnblogs.com/sk-net/archive/2011/09/07/2170224.html 以下的文章主要介绍的是MySQL游标的使用笔记,其可以用在存储过程的S ...

  4. jsp中非空判断

    function中uname要和id的值相匹配,但是这样不专业,要显示我的专业性,我将使用document获得name的值来判断是否为空,应为这样是专业的写法,我要时刻记住我是专业的 <scri ...

  5. POI导出时,将指定的列设置为下拉列表

    本示例设置第2列为下拉框(下拉框内容为:是/否),从第5行开始到5657行结束. 关键代码示例: ComboxList = new String[]{"是","否&quo ...

  6. Jauery 中Ajax的几种异步请求

       以下介绍Jquery中  Post   Get   Ajax几种异步请求的使用方法  <%@ Page Language="C#" AutoEventWireup=&q ...

  7. Objective-C Properties

    Objective-C Properties Apple introduced properties, a combination of new compiler directivesand a ne ...

  8. hibernate 批量插入数据

    如题,有两种方法 1)使用FLUSH 2)使用JDBC 分别来解释: 1)hibernate在进行数据库操作的时候,都要有事务支持的.可能你曾遇到过,没有加事务,程序会报错的情况. 而事务每次提交的时 ...

  9. [Windows Server 2012] 阿里云镜像购买和使用方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将演示:阿里云镜像购买 ...

  10. 数组,寻找第K大的数

    时间复杂度 O(n) def partition(data,left,right): if (len(data)<=0 or left<0 or right>=len(data)): ...