lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上
题目
给出二维平面上的n个点,求最多有多少点在同一条直线上。
给出4个点:(1, 2), (3, 6), (0, 0), (1, 3)。
一条直线上的点最多有3个。
解题
直接暴力求解有问题,时间复杂度O(N3),对其中的相同点没有处理,斜率为0,不存在也没有处理,找出运行不对
看到通过定义一个HashMap存储直线的斜率,存在相同的情况就+ 1 ,最后找到斜率个数最长的那个。
下面程序中已经有很多注释了。
/**
* 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 {
/**
* @param points an array of point
* @return an integer
*/
public int maxPoints(Point[] points) {
// Write your code here
if(points == null)
return 0;
int m = points.length;
if(m == 0)
return 0;
if( m <= 2)
return m;
int maxline = 0;
HashMap<Double,Integer> map = new HashMap<Double,Integer>();
for(int i = 0 ;i < m ; i ++){
map.clear();
// dup 是记录和当前点i相同的点的个数,注意这里不包括当前点 i
int dup = 0;
for(int j = i + 1; j < m ;j++){
// 出来相同的点
if(points[i].x == points[j].x && points[i].y == points[j].y){
dup ++;
continue;
}
// 处理斜率不存在的情况
double k = points[i].x==points[j].x ?Integer.MAX_VALUE:
(0.0+ points[i].y-points[j].y)/(points[i].x-points[j].x)*1.0; if(map.containsKey(k)){
map.put(k,map.get(k)+1); // 这里是新的j点,只需 + 1
}else{
map.put(k,2); // i j 两个点所在的直线,有两个点
}
}
// 全部相同的情况
if(map.size()==0){
// 需要加上当前点
maxline = Math.max(maxline,dup + 1 );
}else{
for(int tmp:map.values()){
if(tmp+dup>maxline)
maxline = tmp+dup; }
} }
return maxline;
}
}
Java Code
# Definition for a point.
# class Point:
# def __init__(self, a=0, b=0):
# self.x = a
# self.y = b class Solution:
# @param {int[]} points an array of point
# @return {int} an integer
def maxPoints(self, points):
# Write your code here
if points == None :
return 0
m = len(points)
if m <= 2:
return m
maxline = 0
for i in range(m):
dup = 1
d = {}
d['inf'] = 0
for j in range((i+1),m):
if points[i].x == points[j].x and points[j].y == points[i].y:
dup +=1
elif points[j].x == points[i].x:
d['inf'] += 1
else:
k = 1.0*(points[j].y - points[i].y)/(points[j].x - points[i].x)
if k in d:
d[k] += 1
else:
d[k] = 1
maxline = max( max(d.values()) + dup,maxline) return maxline
Python Code
lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上的更多相关文章
- 149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- [LintCode] 最多有多少个点在一条直线上
/** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(i ...
- [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. ...
- Max Points on a Line(直线上最多的点数)
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | ...
- [LintCode] 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]Max Points on a Line @ Python
原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/ 题意:Given n points on a 2D plane, find th ...
- 【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 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(hard)☆
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
随机推荐
- php中url传递中文字符,特殊危险字符的解决方法
php中的urldecode,base64_encode函数然后再结合自己写的替换函数来进行安全传递url中文字符,特殊危险字符. 需要在url中传递中文字符或是其它的html等特殊字符,似乎总会有各 ...
- 【转】Linux Framebuffer
全面的framebuffer详解 一.FrameBuffer的原理 FrameBuffer 是出现在 2.2.xx 内核当中的一种驱动程序接口. Linux是工作在保护模式下,所以用户态进程是无法象D ...
- [转]WPF 依赖项属性
from:http://blog.csdn.net/datoumimi/article/details/8033682 ps:环境限制,发的东西一长就会被拦截,所以删了一部分 UI软件中经常会用到大量 ...
- MySQL在ROW模式下通过binlog提取SQL语句
Linux基于row模式的binlog,生成DML(insert/update/delete)的rollback语句通过mysqlbinlog -v 解析binlog生成可读的sql文件提取需要处理的 ...
- mysql 配置主从
1.选择2个ip,1个为主,1个为从:例:主:192.168.12.76 从:192.168.12.772.在192.168.12.76的my.cnf 配置master,添加如下:(红色为添加的内容) ...
- 关于qt5在win7下发布 & 打包
QT5 发布时,莫过于依赖动态链接库(dll) , 但是,QT5的动态链接库貌似都有2套 ,例如 Qt5Core (针对realese) , Qt5Cored (针对debug) ,凡事末尾带d的都是 ...
- nodejs redis
0. install redis library for node npm install redis 1.node command example > var _redis = require ...
- 从InputStream到String_写成函数
String result = readFromInputStream(inputStream);//调用处 //将输入流InputStream变为String public String readF ...
- 清除SQL Server执行计划
有时需要调试SQL语句的性能, 需要不断的执行SQL语句, 可是多次执行同一条语句的时候,SQL Server 会缓存表的数据,结果就测不出来 实际的 SQL 的性能 用以下SQL可以清除缓存数据 D ...
- Asp.Net生命周期系列六
上篇说到当一个Http请求流到HttpHandler这里时才开始对它的处理,那么一个请求经过HttpHandler之后, 到底怎么对它处理呢,也就是说HttpHandler会触发哪些事件,触发的顺序如 ...