Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit02c1c26

Browse files
authored
Added Find Nearest Point That Has the Same X or Y Coordinate.java
1 parent0cf730d commit02c1c26

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
classSolution {
2+
publicintnearestValidPoint(intx,inty,int[][]points) {
3+
int[]basePoint =newint[]{x,y};
4+
returnIntStream.range(0,points.length).mapToObj(i ->newPoint(basePoint,i,points[i]))
5+
.filter(
6+
Point::isMatch)
7+
.sorted(Comparator.comparingInt(Point::getManhattanDistance).thenComparing(Point::getIdx))
8+
.map(Point::getIdx).findFirst().orElse(-1);
9+
}
10+
11+
privatestaticclassPoint {
12+
int[]basePoint;
13+
intidx;
14+
int[]currPoint;
15+
16+
publicPoint(int[]basePoint,intidx,int[]currPoint) {
17+
this.basePoint =basePoint;
18+
this.idx =idx;
19+
this.currPoint =currPoint;
20+
}
21+
22+
publicintgetIdx() {
23+
returnidx;
24+
}
25+
26+
publicintgetManhattanDistance() {
27+
returnMath.abs(currPoint[0] -basePoint[0]) +Math.abs(currPoint[1] -basePoint[1]);
28+
}
29+
30+
publicbooleanisMatch() {
31+
returncurrPoint[0] ==basePoint[0] ||currPoint[1] ==basePoint[1];
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp