leetcode--001 max point on a line
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的更多相关文章
- 【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] 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. ...
- 【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. ...
- 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. ...
- 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. ...
- [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. ...
- [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 ...
- 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 ...
- 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. ...
随机推荐
- InnoDB的数据页结构
页是InnoDB存储引擎管理数据库的最小磁盘单位.页类型为B-tree node的页,存放的即是表中行的实际数据了. InnoDB数据页由以下七个部分组成,如图所示: File Header(文件头) ...
- Javascript模块化编程:AMD规范及require.js用法【转】 - loheonly的笔记 - 前端网(W3Cfuns)
http://www.w3cfuns.com/blog-5425789-5399326.html
- psy & obv
PSY和OBV 1,PSY心理线是投资者对股市涨跌产生心理波动的情绪指标.相反,当PSY曲线向下跌破PSYMA曲线后,为卖出时机..当PSY曲线向上突破PSYMA曲线后,开始向下回调至PSYMA曲线, ...
- windows 2003添加删除windows组件中无iis应用程序服务器项的解决方法
解决方法如下: 1.开始 -- 运行,输入 c:\Windows\inf\sysoc.inf,会打开这个文件;在sysoc.inf中找到"[Components]"这一段,并继续找 ...
- 自己用wireshark 抓了个包,分析了一下
我自把对应 ip 包的头部拿出来手动分析了一下 :)
- js 中ajax请求时设置 http请求头中的x-requestd-with= ajax
今天发现 AngularJS 框架的$http服务提供的$http.get() /$http.post()的ajax请求中没有带 x-requested-with字段. 这样的话,后端的php 就无法 ...
- 面向对象重写(override)与重载(overload)区别
一.重写(override) override是重写(覆盖)了一个方法,以实现不同的功能.一般是用于子类在继承父类时,重写(重新实现)父类中的方法. 重写(覆盖)的规则: 1.重写方法的参数列表必须完 ...
- Struts1.x教程:配置文件总结
要想使用Struts,至少要依靠两个配置文件:web.xml和struts-config.xml.其中web.xml用来安装Struts框架.而struts-config.xml用来配置在Struts ...
- 2016大连网络赛 Function
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Probl ...
- 【贪心】时空定位II
[贪心]时空定位II 题目描述 有一块空间,横向长w,纵向长为h,在它的横向中心线上不同位置处装有n(n≤10000)个点状的定位装置,每个定位装置i定位的效果是让以它为中心半径为Ri的圆都被覆盖.请 ...