package com.lw.leet3;

 import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry; /**
* @ClassName:Solution
* @Description: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.
* @Author LiuWei
* @Date 2014年8月17日下午2:51:08
* @Mail nashiyue1314@163.com
*/ /**
* 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) {
int max = 0;
if(points.length <= 2){
max = points.length;
}
else{
for(int i = 0; i < points.length; i++){
int equalNum = 0;
Map<Double, Integer> map = new HashMap<Double, Integer>();
for(int j = i+1; j < points.length; j++ ){
if(points[i].x == points[j].x && points[i].y == points[j].y){
equalNum ++;
continue;
} double k = 0;
if(points[i].x == points[j].x){
k = Double.MAX_VALUE;
}
else{
k = ((double)(points[i].y - points[j].y)/(points[i].x - points[j].x));
/**
* Submission Result: Wrong Answer
* Input: [(2,3),(3,3),(-5,3)]
* Output: 2
* Expected:3
*
* avoid k = +0.0 -0.0
* */
if(k == 0){
k = 0;
}
} if(map.containsKey(k)){
map.put(k, map.get(k)+1);
}
else{
map.put(k, 2);
}
} /**
* Submission Result: Wrong Answer
* Input: [(1,1),(1,1),(1,1)]
* Output: 0
* Expected:3
*
* avoid the points are all equal
* */
if(equalNum > max){
max = equalNum + 1 ;
}
Iterator<Entry<Double, Integer>> iter = map.entrySet().iterator();
while(iter.hasNext()){
Entry<Double, Integer> entry = iter.next();
int num = entry.getValue();
if( num + equalNum > max){
max = num + equalNum;
}
}
}
}
return max;
} public static void main(String[] args){
Point[] p = {new Point(2, 3),new Point(3,3),new Point(-5,3)};
// Point[] p = {new Point(1, 1),new Point(1,1),new Point(1,1)}; Solution s = new Solution();
System.out.println(s.maxPoints(p)); } }

LeetCode-Max Points on a Line[AC源码]的更多相关文章

  1. 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 on the ...

  2. [LeetCode] 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. ...

  3. [leetcode]Max Points on a Line @ Python

    原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/ 题意:Given n points on a 2D plane, find th ...

  4. [LeetCode] Max Points on a Line 题解

    题意 Given n points on a 2D plane, find the maximum number of points that lie on the same straight lin ...

  5. LeetCode:Max Points on a Line

    题目链接 Given n points on a 2D plane, find the maximum number of points that lie on the same straight l ...

  6. 【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 ...

  7. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  8. 【LeetCode】149. 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 on the ...

  9. [LintCode] 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. ...

随机推荐

  1. php异步学习(2)

    PHP开启异步多线程执行脚本   场景要求 客户端调用服务器a.php接口,需要执行一个长达5s-20s不等的耗资源操作,但是客户端响应请求时间为5秒(微信公众账号服务器请求响应超时时间),5s以上无 ...

  2. c# 调用c++dll二次总结

    1.pinvoke结构不对称,添加语句(网上有) 2.含回调函数,成员参数的结构体必须完全,尽管自己用不到. 3.加深对c++指针的理解.一般情况下,类型加*等效于c++中的ref.但对于short* ...

  3. pandas中DataFrame的ix,loc,iloc索引方式的异同

    pandas中DataFrame的ix,loc,iloc索引方式的异同 1.loc: 按照标签索引,范围包括start和end 2.iloc: 在位置上进行索引,不包括end 3.ix: 先在inde ...

  4. HDU 5206 Four Inages Strategy 水题

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5206 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  5. Java中static关键字的作用和用法详细介绍

    static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和成员方法独立于该类的任何 ...

  6. winform 弹出窗体指定位置

    在启动一个程序时,我们希望窗口显示的位置处于屏幕的正中心,可以如下设置: MainForm mainForm = new MainForm(); mainForm.StartPosition = Fo ...

  7. [cnbeta]微软最强数据中心级操作系统

    微软近日发表了一篇介绍Windows系统内核的博文,期间为了展示Windows的强大扩展性,放出了一张非常震撼的Windows任务管理器截图:乍一看似乎没啥特别的,几十甚至上百个逻辑核心的系统并不罕见 ...

  8. php面试必知必会常见问题

    1 说出常用的10个数组方法 我觉得数组比较最能体现PHP基础语法的一个数据结构了,下面给大家列一下常用的10个关于操作数组的函数 in_array(判断数组中是否有某个元素) implode(将数组 ...

  9. c# assembly

    string path = @"c:\text.dll" Assembly assembly = Assembly.LoadFile(path); path = "MyP ...

  10. Struts访问序号的设置