-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution1496.go
More file actions
53 lines (48 loc) · 868 Bytes
/
Copy pathsolution1496.go
File metadata and controls
53 lines (48 loc) · 868 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
47
48
49
50
51
52
53
package solution1496
// ============================================================================
// 1496. Path Crossing
// URL: https://leetcode.com/problems/path-crossing/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_isPathCrossing-24 21440473 92.86 ns/op 0 B/op 0 allocs/op
PASS
*/
func isPathCrossing(path string) bool {
type Point struct {
x, y int
}
x, y := 0, 0
m := make(map[Point]int)
p := Point{
x: x,
y: y,
}
m[p] = 1
for _, v := range path {
switch v {
case 'N':
y--
case 'S':
y++
case 'W':
x--
case 'E':
x++
}
p = Point{
x: x,
y: y,
}
_, ok := m[p]
if ok {
return true
} else {
m[p] = 1
}
}
return false
}