File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed
Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+
4+ class Solution {
5+ static class Coords {
6+ int x1, y1, x2, y2;
7+ public Coords (int x1 ,int y1 , int x2 , int y2 ){
8+ this . x1 = x1;
9+ this . y1 = y1;
10+ this . x2 = x2;
11+ this . y2 = y2;
12+ }
13+ @Override
14+ public boolean equals (Object o ){
15+ if (! (o instanceof Coords ))
16+ return false ;
17+ Coords c = (Coords )o;
18+ return (this . x1 == c. x1 && this . y1 == c. y1 && this . x2 == c. x2 && this . y2 == c. y2) || (this . x1 == c. x2 && this . y1 == c. y2 && this . x2 == c. x1 && this . y2 == c. y1);
19+ }
20+
21+ @Override
22+ public int hashCode (){
23+ // 이렇게 순서를 맞춰줘야 함
24+ int sx1 = Math . min(x1, x2);
25+ int sx2 = Math . max(x1, x2);
26+ int sy1 = Math . min(y1, y2);
27+ int sy2 = Math . max(y1, y2);
28+ return Objects . hash(sx1, sy1, sx2, sy2);
29+ // return Objects.hash(x1, x2, y1, y2);
30+ }
31+ }
32+
33+ public int solution (String dirs ) {
34+ int answer = 0 ;
35+ HashSet<Coords > set = new HashSet<> ();
36+ int x = 0 ;
37+ int y = 0 ;
38+ for (char d: dirs. toCharArray()){
39+ int nx = x;
40+ int ny = y;
41+ if (d == ' U' )
42+ ny++ ;
43+ else if (d == ' D' )
44+ ny-- ;
45+ else if (d == ' R' )
46+ nx++ ;
47+ else if (d == ' L' )
48+ nx-- ;
49+
50+ if (nx < - 5 || nx > 5 || ny < - 5 || ny > 5 )
51+ continue ;
52+ set. add(new Coords (x, y, nx, ny));
53+ x = nx;
54+ y = ny;
55+ }
56+ answer = set. size();
57+ return answer;
58+ }
59+ }
60+
61+ ```
You can’t perform that action at this time.
0 commit comments