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. ...
随机推荐
- Windows API 之 FineFirstFile、FindNextFile
参考:https://msdn.microsoft.com/en-us/library/aa364418%28VS.85%29.aspx FindFirstFile Searches a direct ...
- L2,breakfast or lunch
express: what a day多么糟糕的天气 I‘m coming to see you我将要来看你 what a lot of trouble he is causing他犯了多少错误啊 w ...
- hdu_5085_Counting problem(莫队分块思想)
题目连接:hdu_5085_Counting problem 题意:给你一个计算公式,然后给你一个区间,问这个区间内满足条件的数有多少个 题解:由于这个公式比较特殊,具有可加性,我们考虑讲一个数分为两 ...
- 提升html5的性能体验系列之五webview启动速度优化及事件顺序解析]
webview加载时有3个事件.触发顺序为loading.titleUpdate.loaded.webview开始载入页面时触发loading,载入过程中如果title已经解析并赋予新值,则触发tit ...
- Android Studio中有没有类似于Eclipse中的ctrl+2+L的快捷键? \Android Studio快捷键之代码提示
问:Android Studio中有没有类似于Eclipse中的ctrl+2+L的快捷键? 答:有,as中的快捷键是Ctrl+Alt+V AndroidStudio和Eclipse常用快捷键对比 功能 ...
- Android编程之SparseArray<E>详解
最近编程时,发现一个针对HashMap<Integer, E>的一个提示: 翻译过来就是:用SparseArray<E>来代替会有更好性能.那我们就来看看源码中SparseAr ...
- 设置span 宽度的完美解决方案
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Blocks(闭包)
转自:http://my.oschina.net/joanfen/blog/317644?fromerr=ATWzC3Y2 Block 与传统代码相比较更加轻量,调用简洁方便,而且可以返回多个参数,使 ...
- IDEA github的应用
1.下载并安装一个git 一直点下一步就可以 2.去github官网注册一个账号 https://github.com/ 2.1点击Sign up 进入注册页面 2.2填写注册信息,点击Create ...
- Monkey and Banana(基础DP)
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...