由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Leetcode ==> Max Points on a Line, 我的程序到底哪出问题了
相关主题
问个set和literal String的问题Skyline
问一道关于Vector的题override/overload/overwrite in Java
问HashSet的问题?Re: 谁有Java或Oracle的毒招 ?
answer Re: how HashMap/Hashtable compare key?Re: hashtable
Object比较[转载] How do I sort a map in Java?
问个Object.hashCode()的问题 ObjectOutputStream.writeObject(Hashtable) ?
问个hashtable实现问题大家写java class的时候是完全封装的么?
treemap和hashma p的问题请教一个多线程lock机制的问题
相关话题的讨论汇总
话题: point话题: line话题: int话题: ptset话题: return
进入Java版参与讨论
1 (共1页)
S******1
发帖数: 216
1
/**
* 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 {

class Line {
boolean vertical = false;
double slop = 0.0;
double intercept = 0.0;

public Line(Point pt1, Point pt2) {
if (pt1.x == pt2.x)
{
vertical = true;
intercept = pt1.x;
}
else
{
slop = (double)(pt1.y - pt2.y)/(double)(pt1.x - pt2.x);
intercept = pt1.y - slop*pt1.x;
}
}

@Override
public boolean equals(Object obj) {

if (obj == null || !(obj instanceof Line))
return false;

Line l = (Line)obj;
if (this.vertical != l.vertical)
return false;

final double precision = 0.0001;

return Math.abs(this.intercept - l.intercept) < precision &&
Math.abs(this.slop - l.slop) < precision;
}

@Override
public int hashCode() {
long tmp1 = Double.doubleToRawLongBits(slop);
long tmp2 = Double.doubleToRawLongBits(intercept);

if (!vertical)
{
long tmp = tmp1 ^ tmp2;
return (int)tmp ^ (int)(tmp >> 32);
}

return (int)tmp2 ^ (int)(tmp2 >> 32);
}
}

public int maxPoints(Point[] points) {

if (points.length <= 0)
return 0;

int ret = 1;
Map> map = new HashMap>();
for (int i = 0; i < points.length; i++) {
for (int j = i+1; j < points.length; j++) {
Line l = new Line(points[i], points[j]);
Set ptSet = null;
if (!map.containsKey(l))
ptSet = new HashSet();
else
ptSet = map.get(l);

ptSet.add(points[i]);
ptSet.add(points[j]);
map.put(l, ptSet);

ret = Math.max(ret, ptSet.size());
}
}

return ret;
}
}
为什么这个会 fail一个test case, 返回21, expected 22, 到底哪出问题了??
1 (共1页)
进入Java版参与讨论
相关主题
请教一个多线程lock机制的问题Object比较
How to check if an element is in an array?问个Object.hashCode()的问题
Do I need to implement equals and hashCode in domain object问个hashtable实现问题
TIJ上写错了?treemap和hashma p的问题
问个set和literal String的问题Skyline
问一道关于Vector的题override/overload/overwrite in Java
问HashSet的问题?Re: 谁有Java或Oracle的毒招 ?
answer Re: how HashMap/Hashtable compare key?Re: hashtable
相关话题的讨论汇总
话题: point话题: line话题: int话题: ptset话题: return