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. Python 函数内省

    函数内省(function introspection) 除了__doc__属性, 函数对象还有很多属性,对于下面的函数,可以使用dir()查看函数具有的属性: def factorial(n): r ...

  2. 【转】utf-8与Unicode的转化

    作者:uuspider链接:https://www.zhihu.com/question/23374078/answer/65352538来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...

  3. Mysql中``和‘’的区别

    两者在linux下和windows下不同,linux下不区分,windows下区分. 在windows下主要区别就是 单引号( ' )或双引号主要用于 字符串的引用符号 如: mysql> SE ...

  4. ACM-ICPC 2018 沈阳赛区网络预赛

    Supreme Number 1000ms 131072K   A prime number (or a prime) is a natural number greater than 111 tha ...

  5. lintcode-421-简化路径

    421-简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./ ...

  6. CentOS系统iptables防火墙的启动、停止以及开启关闭端口的操作

    CentOS 配置防火墙操作实例(启.停.开.闭端口):注:防火墙的基本操作命令:查询防火墙状态:[root@localhost ~]# service   iptables status停止防火墙: ...

  7. 零拷贝Zero-Copy(NIO)

    介绍 Java 的zero copy多在网络应用程序中使用.Java的libaries在linux和unix中支持zero copy,关键的api是java.nio.channel.FileChann ...

  8. PC和FPGA间的串口通信实现

    应用笔记 V1.0 2015/03/26 PC和FPGA间的串口通信实现   概述   本文将介绍PC和FPGA间的串口通信实现的基本思路和Verilog代码,对于通信而言,收发双方都要有相应的控制. ...

  9. CSS布局-垂直居中问题

    在前端页面的布局方面,很多时候需要用到垂直居中,如果父集元素或子元素的高度是固定的那么垂直居中可以用line-height或者定位 top:50%:margin-top:- height/2 px:实 ...

  10. 【Mybatis】简单的mybatis增删改查模板

    简单的mybatis增删改查模板: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...