-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1496. Path Crossing
More file actions
46 lines (34 loc) · 788 Bytes
/
1496. Path Crossing
File metadata and controls
46 lines (34 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public boolean isPathCrossing(String path)
{
int x = 0, y = 0;
HashSet<String> visited = new HashSet<>();
visited.add("0,0");
for (char dir : path.toCharArray())
{
if (dir == 'E')
{
x++;
}
else if (dir == 'W')
{
x--;
}
else if (dir == 'N')
{
y++;
}
else if (dir == 'S')
{
y--;
}
String curr = x + "," + y;
if (visited.contains(curr))
{
return true;
}
visited.add(curr);
}
return false;
}
}