package leetcode;

 import java.util.HashMap;

 class Point{
int x;
int y;
Point(){
x=0;
y=0;
}
Point(int a,int b){
x=a;
y=b;
}
} public class MaxPointOnALine {
public static int maxPoints(Point[] points) { if(points.length<=2) {
return points.length;
}
//斜率
double k = 0.0;
int maxPointNum = 0;
int parallelNum =0;
int tempMaxPointNum = 0;
int sameNum=0;
HashMap map=new HashMap<Double,Integer>();
for(int i=0;i<points.length;i++){
sameNum=1;
parallelNum=0;
tempMaxPointNum=0;
map.clear();
for(int j=i+1;j<points.length;j++){
if(points[i].x==points[j].x&&points[i].y==points[j].y){
sameNum++;
}
else if(points[i].x==points[j].x){
parallelNum++; }else {
if(points[i].y==points[j].y){
k=0.0; }else{
k=((double)(points[i].y-points[j].y))/(points[i].x-points[j].x);
}
if(map.get(k)==null){
map.put(k, new Integer(1));
if(1>tempMaxPointNum){
tempMaxPointNum=1;
}
}else{
int number=(int)map.get(k);
number++;
map.put(k, number);
if(number>tempMaxPointNum){
tempMaxPointNum=number;
}
}
}
}
if(parallelNum>1){
if(parallelNum>tempMaxPointNum)
tempMaxPointNum=parallelNum;
}
tempMaxPointNum+=sameNum;
if(tempMaxPointNum>maxPointNum)
maxPointNum=tempMaxPointNum;
}
return maxPointNum;
}
public static void main(String[] args){
Point[] parr = {new Point(1,1),new Point(1,2)};
//maxPoints(parr);
System.out.println(maxPoints(parr));
}
}

leetcode--001 max point on a line的更多相关文章

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

  2. [LeetCode OJ] Max Points on a Line

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

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

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

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

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

  8. [LeetCode OJ] 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.

    //定义二维平面上的点struct Point { int x; int y; Point(, ):x(a),y(b){} }; bool operator==(const Point& le ...

  9. LeetCode之Max Points on a Line Total

    1.问题描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight ...

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

随机推荐

  1. Python -- lambda, map, filter

    lambda f = lambda x : x * 2 f(5) f = lambda x,y,z : x+y+z f(2,1,3) map list(map(lambda x:x[0].upper( ...

  2. Effective java -- 4 泛型

    第二十三条:请不要在代码中使用原生态类型就是像Set这种待泛型的,就把泛型明确写出来. 第二十四条:消除非受检警告就是Set<String> sets = new HashSet();这种 ...

  3. zencart 新页面调用好功能代码集:

    其实很多就是看变量,就可以直接调用,而变量的定义地方很多,比如language 1.  includes\languages\语言.php 2. 写个文件,放进includes\extra_confi ...

  4. select 通过表单提交获取select中的值

    <select class="txt" name="choice">       <option value="name" ...

  5. 尝试在数据库 5 中提取逻辑页 (1:1640) 失败。该逻辑页属于分配单元XXX ,而非 XXX。

    此信息表明数据库或表 已经部分损坏可以通过以下步骤尝试修复: 1. DBCC CHECKDB 重启服务器后,在没有进行任何操作的情况下,在SQL查询分析器中执行以下SQL进行数据库的修复,修复数据库存 ...

  6. 计算n!的位数<Math>

    题意:如题目. 方法一:<TLE> * 可设想n!的结果是不大于10的M次幂的数,即n!<=10^M(10的M次方),则不小于M的最小整数就是 n!的位数,对 * 该式两边取对数,有 ...

  7. Linux字符编码转换 UTF8转GB3212

    在LINUX上进行编码转换时,既可以利用iconv函数族编程实现,也可以利用iconv命令来实现,只不过后者是针对文件的,即将指定文件从一种编码转换为另一种编码.    一.利用iconv函数族进行编 ...

  8. c# 动态产生控件 注册动态控件事件

    用CheckEdit演示 其他控件类推 CheckEdit AllSele = new CheckEdit(); AllSele.Location = new System.Drawing.Point ...

  9. html ajax 双引号的问题

    "<a onClick='openwin_searchmodel()' href='searchmodel.action?projectname="+person[i].ge ...

  10. 深度学习 for java http://deeplearning4j.org/

    http://deeplearning4j.org/ http://deeplearning4j.org/zh-neuralnet-overview