Skip to content

Commit 07c3535

Browse files
authored
Create 1496-path-crossing.kt
1 parent dca6518 commit 07c3535

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

kotlin/1496-path-crossing.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
fun isPathCrossing(path: String): Boolean {
3+
var dirs = mapOf(
4+
'N' to intArrayOf(0, 1),
5+
'S' to intArrayOf(0, -1),
6+
'E' to intArrayOf(1, 0),
7+
'W' to intArrayOf(-1, 0),
8+
)
9+
val visit = HashSet<Pair<Int, Int>>()
10+
var x = 0
11+
var y = 0
12+
13+
for (c in path) {
14+
visit.add(x to y)
15+
val (dx, dy) = dirs[c]!!
16+
x += dx
17+
y += dy
18+
if ((x to y) in visit)
19+
return true
20+
}
21+
22+
return false
23+
}
24+
}

0 commit comments

Comments
 (0)