[leedcode 149] Max Points on a Line
2015-08-01 18:37
375 查看
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
/** * 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 { public int maxPoints(Point[] points) { //三层遍历,依次判断是否这些点在一条线路。注意判断两个相同的点的情况。 //相同点的个数使用一个计数器dup,非相同但在一条直线上也有一个变量cur if(points==null||points.length<=0) return 0; int len=points.length; int dup=1; int cur=1; int res=0; for(int i=0;i<len;i++){ dup=1; for(int j=i+1;j<len;j++){ if(points[i].x==points[j].x&&points[i].y==points[j].y){//相同点 dup++; continue; } for(int k=j+1;k<len;k++){ if(isSameLine(points[i],points[j],points[k])){ cur++; } } if(res<cur+dup)res=cur+dup; cur=1; } if(res<dup)res=dup;//注意都是相同点的处理 cur=1; } return res; } public boolean isSameLine(Point i,Point j,Point k){//相同点的判断,斜率相同 if((i.x-j.x)*(i.y-k.y)==(i.x-k.x)*(i.y-j.y)) return true; else return false; } }
相关文章推荐
- 详解CSS中的类和ID选择符
- BestCoder 1st Anniversary 1003(HDU5312)
- Web应用的组件化开发
- hdu 2111 saving HDU
- 初学JAVA简谈字符串String存储的以及字符串值的比较(==运算符以及equals())
- 深入理解CSS中的UI伪类
- 使用GDB调试程序
- main Thread ,worker Thread
- (一〇〇)使用AddressBookUI实现通讯录操作
- (一〇〇)使用AddressBookUI实现通讯录操作
- [Github开源库PinterestLikeAdapterView]--Android瀑布流的实现
- 寻找素数
- 【翻译自mos文章】rman 标准版和企业版的兼容性
- ABP应用层——应用服务(Application services)
- phonegap中 app 开发的那些坑~~!!
- Java 提取文件编码
- PAT IO-04 混合类型数据格式化输入(5)
- 可喜
- Application的生命周期简介
- 第十七章、LVM