LeetCode-Max Points on a Line[AC源码]
来源:互联网 发布:唱歌声卡软件下载 编辑:程序博客网 时间:2024/06/05 11:26
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)); } }
自己写的时候,出了以下几点问题:
1.与x轴平行情况下,Map区分+0.0与-0.0,导致数据出错,但是不管+0.0与-0.0值是相等的,就是用+0.0 == -0.0条件判断的时候,是true。
注:由于Map在Put数据的时候,对Key的判断是进行equal运算,而从下面的Double类的equal源码可以看到,它并不是简简单单的比较两个数据的值是否相等,而是将其调用doubleToLongBits方法,比较该方法返回的值是否相等。将这两个值输出,如下所示:
-9223372036854775808
0
由此,不难理解Map为什么区分+0.0与-0.0~
public static long doubleToLongBits(double value) {long result = doubleToRawLongBits(value);// Check for NaN based on values of bit fields, maximum// exponent and nonzero significand.if ( ((result & DoubleConsts.EXP_BIT_MASK) ==DoubleConsts. EXP_BIT_MASK) &&(result & DoubleConsts. SIGNIF_BIT_MASK) != 0L)result = 0x7ff8000000000000L;return result;}
public boolean equals(Object obj) {return (obj instanceof Double)&& ( doubleToLongBits(((Double)obj).value) ==doubleToLongBits(value));}
2.当所有的点都一样的时候,由于map的Size为0,不进入循环,但是这若干个也算作在一条直线上。所以在前面进行了处理~
总体思想:
利用平面中,点+斜率确定一条直线的思想。当固定某一个顶点的时候,如果斜率相等,那么这个直线唯一确定。
请注意下,变量equalNum
0 0
- LeetCode-Max Points on a Line[AC源码]
- Max Points on a Line | leetcode
- Max Points on a Line LEETCODE
- Leetcode: Max Points on a Line
- LeetCode - Max Points on a Line
- LeetCode 之 Max Points on a Line
- LeetCode | Max Points on a Line
- [LeetCode] Max Points on a Line
- Leetcode: Max Points on a Line
- LeetCode OJ:Max Points on a Line
- Leetcode Max Points on a Line
- Leetcode: Max Points on a Line .
- [Leetcode] Max Points on a Line (Java)
- leetcode:【Max Points on a Line】
- Max Points on a Line - LeetCode
- LeetCode:Max Points on a Line
- [LeetCode] Max Points on a Line
- leetcode - Max Points on a Line
- 七步从AngularJS菜鸟到专家(6):服务(1)
- poj1815+sap+枚举+最小割点集
- 理解互斥量和信号量 .
- 七步从AngularJS菜鸟到专家(7):Routing(1)
- [python]去除列表中重复元素
- LeetCode-Max Points on a Line[AC源码]
- 目标跟踪的点跟踪技术(4)
- 隐藏office web app打印和在word中打开等功能
- java的多态
- zoj 1654 Place the Robots 二分图匹配
- linux内核之hlist哈希链表的应用---C语言代码实现(内核态)
- PAT_B_字符串-05. 字符串循环左移(20)
- 二叉树查找删除
- Map日常--遍历Map,Map转Lis