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. ES6的新特性(10)——Class 的基本语法

    Class 的基本语法 简介 JavaScript 语言中,生成实例对象的传统方法是通过构造函数.下面是一个例子. function Point(x, y) { this.x = x; this.y ...

  2. 第9次Scrum会议(10/21)【欢迎来怼】

    一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华小组照片 二.开会信息 时间:2017/10/21 17:20~17:45,总计25min.地点:东北师范 ...

  3. 各团队对《t铁大导航》评价及我组回复

    组名 对我们组的建议 我组回复 (1)跑男 你们的导航前期要做到什么样的程度呢?其实我一直是很期待你们完成时我能够用你们的导航来感受一下咱们铁大校园风情.你们用了数据结构算法来找最短路径,那你们能不能 ...

  4. QJsonDocument实现Qt下JSON文档读写

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QJsonDocument实现Qt下JSON文档读写     本文地址:http://tech ...

  5. 【beta】nice!-------约吧NABCD

    小组名称:nice! 组长:李权 成员:于淼  刘芳芳韩媛媛 宫丽君 项目内容:约跑app(约吧) 约吧APP下载地址: 百度云:链接:http://pan.baidu.com/s/1jHNBR3g ...

  6. ajax跨域问题(三种解决方案)

    为什么会出现跨域 跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其 ...

  7. WordPress使用淘宝IP地址库的API显示评论者的位置信息(二)

    1 淘宝IP地址库的接口说明 在上一篇文章<WordPress使用淘宝IP地址库的API显示评论者的位置信息(一)>中,vfhky使用了新浪工具提供的这个IP接口显示博客评论者的位置信息. ...

  8. 10个linux网络和监控命令

    我下面列出来的10个基础的每个linux用户都应该知道的网络和监控命令.网络和监控命令类似于这些: hostname, ping, ifconfig, iwconfig, netstat, nsloo ...

  9. socket与TCP/UDP编程~

    ket接口是TCP/IP网络的API,Socket接口定义了许多函数或例程,程序员可以用它们来开发TCP/IP网络上的应用程序.要学Internet上的TCP/IP网络编程,必须理解Socket接口. ...

  10. Maven学习——1、安装与修改Maven的本地仓库路径

    1.1.下载 官网 http://maven.apache.org/download.cgi 1.2.安装配置 apache-maven-3.3.3-bin.zip 解压下载的压缩包 1.3.配置环境 ...