Skip to content

Instantly share code, notes, and snippets.

@rainiera
Created February 19, 2015 22:22
Show Gist options
  • Save rainiera/1a9bc406dc2920eab13d to your computer and use it in GitHub Desktop.
Save rainiera/1a9bc406dc2920eab13d to your computer and use it in GitHub Desktop.
example of PointDistance class for "KClosestPoints" problem
class PointDistance implements Comparable<PointDistance> {
Point p;
double dist;
public PointDistance(Point p, double dist) {
this.p = p;
this.dist = dist;
}
public int compareTo(PointDistance other) {
return (int) (this.dist - other.dist);
}
// I could fix the toString to display a comma.
// Easy fencepost stuff.
// I will leave it as an exercise to the reader.
public String toString() {
return "(" + (int) p.getX() + ", " + (int) p.getY() + ") ";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment