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, 到底哪出问题了??
* 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
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
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, 到底哪出问题了??