Redian新闻
>
Leetcode ==> Max Points on a Line, 我的程序到底哪出问题了
avatar
Leetcode ==> Max Points on a Line, 我的程序到底哪出问题了# Java - 爪哇娇娃
S*1
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, 到底哪出问题了??
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。