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. UVA - 437 The Tower of Babylon(dp-最长递增子序列)

    每一个长方形都有六种放置形态,其实可以是三种,但是判断有点麻烦直接用六种了,然后按照底面积给这些形态排序,排序后就完全变成了LIS的问题.代码如下: #include<iostream> ...

  2. opencart配置United States Postal Service快递

    1.安装United States Postal Service 2.登录https://registration.shippingapis.com/,注册帐号,稍后会收到邮件 3.打开邮件,记下Us ...

  3. h5的瀑布流

    <!doctype html><html><head><meta charset="utf-8"><title>超简易瀑 ...

  4. php报错 Call to undefined function mb_stripos()

    错误原因 没有mbstring扩展 本文只介绍Linux解决办法 方法一 编译PHP的时候 带上--enable-mbstring参数 方法二 进入PHP源码/ext/mbstring目录 ./con ...

  5. 用telnet命令,SMTP发送邮件

    邮件的发送是基于smtp协议的.邮件客户端软件给smtp服务器传送邮件和smtp服务器之间传送邮件也都是基于smtp协议的.邮件客户端软件接受邮件是主要基于pop3协议的. 下面介绍利用windows ...

  6. 【floyd】 poj 2240

    #include <iostream> #include <map> #include <string> #include <memory.h> usi ...

  7. Andorid APK反逆向解决方案---梆梆加固原理探寻

    本文章由Jack_Jia编写,转载请注明出处.   文章链接:http://blog.csdn.net/jiazhijun/article/details/8892635 作者:Jack_Jia    ...

  8. Ubuntu + VMware=Linux虚拟机

    1.工具 2.要点 3.问题 有时间再写

  9. Web工程师必备的43款可视化工具

    国外站点DATAVISUALIZATION.CH为大家总结出了当前热用的43款可视化工具,包括Arbor.Chroma.js.D3.js.Google Chart Tools等,绝对让你一饱眼福. 1 ...

  10. 根据html生成Word文件,包含图片

    根据html内容生成word,并自动下载下来.使用到了itext-1.4.6.jar import java.io.File; import java.io.FileInputStream; impo ...