lintcode(186)最多有多少个点在一条直线上
来源:互联网 发布:淘宝运营讲师 编辑:程序博客网 时间:2024/05/01 21:36
Description;
给出二维平面上的n个点,求最多有多少点在同一条直线上。
Explanation:
给出4个点:(1, 2)
, (3, 6)
, (0, 0)
, (1, 3)
。
一条直线上的点最多有3个。
Solution:计算斜率冰雹存在hashmap中,注意 xi = xj 的情况 以及两个点相等的情况。
/** * 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 { /** * @param points an array of point * @return an integer */ public int maxPoints(Point[] points) { // Write your code here if(points == null || points.length == 0){ return 0; } int result = 1; HashMap<Double , Integer> record = new HashMap<Double , Integer>(); for(int i = 0; i<points.length;i++){ int same = 0; record.clear(); record.put((double)Integer.MIN_VALUE , 1); for(int j = i+1;j<points.length;j++){ if(points[j].x == points[i].x && points[j].y == points[i].y){ same++; continue; } double k = 0; if(points[j] == points[i]){ k = (double)Integer.MAX_VALUE; }else{ k = (double)(points[j].y - points[i].y)/(double)(points[j].x - points[i].x); } if(record.containsKey(k)){ record.put(k , record.get(k) + 1); }else{ record.put(k , 2); } } for(int count : record.values()){ if(count + same > result){ result = count + same; } } } return result; }}
阅读全文
0 0
- 【LintCode】最多有多少个点在一条直线上
- LintCode:最多有多少个点在一条直线上
- lintcode,最多有多少个点在一条直线上
- LintCode 最多有多少个点在一条直线上
- 最多有多少个点在一条直线上-LintCode
- lintcode最多有多少个点在一条直线上
- lintcode(186)最多有多少个点在一条直线上
- [LintCode 186] 最多有多少个点在一条直线上(Python)
- LinkCode-最多有多少个点在一条直线上
- 最多有多少个点在一条直线上
- Lint_Code_最多有多少个点在一条直线上
- 最多有多少个点在一条直线上
- lintcode-有多少个点在同一条直线上-186
- 解题报告:最多有多少个点在一条直线上
- 最多有多少个点在同一条直线上
- 186.Max Points on a Line-最多有多少个点在一条直线上(中等题)
- 网易互娱笔试题----最多有多少点在一条直线上
- 二维平面上最多有多少个点在同一条直线上
- 管理用户配置文件++查询用户配置文件+修改用户配置文件+删除用户配置文件
- LINUX发展趣文
- C++多态:深入虚函数,理解晚绑定
- select2插件 空格 分词 模糊匹配 模糊搜索
- Java的引用数据类型(重要,面试)
- lintcode(186)最多有多少个点在一条直线上
- RichEdit的各个版本简介
- 资源列表
- IDEA破解 2017 IDEA license server 激活(可用)
- dp——洛谷P1133 教主的花园
- JVM进阶(一)——初识JAVA栈
- Redis in Python:远程连接Redis服务器
- 蓝桥杯决赛-----显示二叉树
- jQuery选择器