-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
14.3 #241
base: master
Are you sure you want to change the base?
14.3 #241
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,10 @@ | |
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class Point { | ||
public class Point implements Comparable<Point>{ | ||
|
||
private final int x; | ||
private final int y; | ||
|
@@ -53,7 +54,10 @@ public String toString() { | |
|
||
// 按照先x再y,从小到大的顺序排序 | ||
// 例如排序后的结果应该是 (-1, 1) (1, -1) (2, -1) (2, 0) (2, 1) | ||
public static List<Point> sort(List<Point> points) {} | ||
public static List<Point> sort(List<Point> points) { | ||
Collections.sort(points); | ||
return points; | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
List<Point> points = | ||
|
@@ -65,4 +69,21 @@ public static void main(String[] args) throws IOException { | |
new Point(2, -1)); | ||
System.out.println(Point.sort(points)); | ||
} | ||
|
||
@Override | ||
public int compareTo(Point point) { | ||
if(this.x<point.x){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return -1; | ||
} | ||
if(this.x>point.x){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return 1; | ||
} | ||
if(this.y<point.y){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return -1; | ||
} | ||
if(this.y>point.y){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return 1; | ||
} | ||
return 0; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'>' 后字符不合法。
'{' 前应有空格。