LinkCode-最多有多少个点在一条直线上
来源:互联网 发布:MOV手机播放软件 编辑:程序博客网 时间:2024/05/01 23:26
最多有多少个点在一条直线上
给出二维平面上的n个点,求最多有多少点在同一条直线上。
样例
给出4个点:(1, 2), (3, 6), (0, 0), (1, 3)。
一条直线上的点最多有3个。
问题分析:
可以根据A点跟其他点所构成线段的斜率判断与A点共线的点的个数.遍历所有点,取出最大值即可,
1.考虑重复点的情况.
2.考虑斜率为无穷大的情况.
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 Solution {//判断坐标点集是否为空 public int maxPoints(Point[] points) { if(points==null||points.length==0){ return 0; }//建立Hash表 HashMap<Double,Integer> map= new HashMap<Double,Integer>(); int max=1; for(int i=0;i<points.length;i++){ map.clear(); //每次重设A点时,清空Hash表 map.put((double)Integer.MIN_VALUE,1); //把相同点的K设为MIN_VALUE; int dup=0; for(int j=i+1;j<points.length;j++){ //判断点是否重合 if(points[j].x==points[i].x&&points[j].y==points[i].y){ dup++; continue; } //判断斜率的K值. double key=points[j].x-points[i].x==0? Integer.MAX_VALUE: 0.0+(double)(points[j].y-points[i].y)/(double)(points[j].x-points[i].x); if(map.containsKey(key)){ map.put(key,map.get(key)+1); }else{ map.put(key, 2); } } for(int temp:map.values()){ if(temp+dup>max){ max=temp+dup; } } } return max; }}
0 0
- LinkCode-最多有多少个点在一条直线上
- 【LintCode】最多有多少个点在一条直线上
- 最多有多少个点在一条直线上
- LintCode:最多有多少个点在一条直线上
- Lint_Code_最多有多少个点在一条直线上
- 最多有多少个点在一条直线上
- lintcode,最多有多少个点在一条直线上
- LintCode 最多有多少个点在一条直线上
- 最多有多少个点在一条直线上-LintCode
- lintcode最多有多少个点在一条直线上
- 解题报告:最多有多少个点在一条直线上
- lintcode(186)最多有多少个点在一条直线上
- [LintCode 186] 最多有多少个点在一条直线上(Python)
- 最多有多少个点在同一条直线上
- 186.Max Points on a Line-最多有多少个点在一条直线上(中等题)
- 网易互娱笔试题----最多有多少点在一条直线上
- 二维平面上最多有多少个点在同一条直线上
- [leetcode] Max Points on a Line 判断最多有多少个点在同一条直线上
- 贝叶斯分类器(Normal Bayes 分类器)
- HDU 1106 排序(水~)
- 三分钟理解“外观模式”——设计模式轻松掌握
- 用ul、li做横向导航
- (java)仙剑人物地图与人物坐标的理解
- LinkCode-最多有多少个点在一条直线上
- 零基础学习Sencha Touch(资料和教程集合)
- 以此勉励
- OSPF区域MD5认证
- 数据库插入微信表情错误
- Android App 开源项目使用统计
- Struts+Spring+Hibernate处理Lob(Blob,Clob)-->sessionFactory中注入 org.springframework.jdbc.support.lob.Def
- MarkdownPad this view has crashed
- 51nod 1378 夹克老爷的愤怒(树形DP+贪心)