Skip to content

Commit 3e2054d

Browse files
Create 1496-path-crossing.java
1 parent 07c3535 commit 3e2054d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

java/1496-path-crossing.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean isPathCrossing(String path) {
3+
Set<String> set = new HashSet<>();
4+
set.add("[0, 0]");
5+
int[] pos = {0, 0};
6+
7+
for(char c: path.toCharArray()){
8+
if(c == 'N'){
9+
pos[1] += 1;
10+
}
11+
else if(c == 'S')
12+
pos[1] -= 1;
13+
else if(c == 'E')
14+
pos[0] += 1;
15+
else
16+
pos[0] -= 1;
17+
if(set.contains(Arrays.toString(pos)))
18+
return true;
19+
set.add(Arrays.toString(pos));
20+
}
21+
return false;
22+
}
23+
}

0 commit comments

Comments
 (0)