Skip to content

Commit 5b55de1

Browse files
committed
feat: add solutions to lc problem: No.1496
No.1496.Path Crossing
1 parent 1db6307 commit 5b55de1

File tree

6 files changed

+169
-165
lines changed

6 files changed

+169
-165
lines changed

solution/1400-1499/1496.Path Crossing/README.md

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:哈希表**
49+
50+
我们可以用一个哈希表 $vis$ 记录路径上的点。初始时,$vis$ 中只有原点 $(0, 0)$。
51+
52+
遍历字符串 $path$,对于每个字符 $c$,根据 $c$ 的值更新当前位置 $(i, j)$,然后判断 $(i, j)$ 是否在 $vis$ 中,如果在,则返回 `true`,否则将 $(i, j)$ 加入 $vis$ 中。
53+
54+
遍历结束后,返回 `false`
55+
56+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $path$ 的长度。
57+
4858
<!-- tabs:start -->
4959

5060
### **Python3**
@@ -54,20 +64,21 @@
5464
```python
5565
class Solution:
5666
def isPathCrossing(self, path: str) -> bool:
57-
x = y = 0
58-
vis = {(x, y)}
67+
i = j = 0
68+
vis = {(0, 0)}
5969
for c in path:
60-
if c == 'N':
61-
y += 1
62-
elif c == 'S':
63-
y -= 1
64-
elif c == 'E':
65-
x += 1
66-
else:
67-
x -= 1
68-
if (x, y) in vis:
70+
match c:
71+
case 'N':
72+
i -= 1
73+
case 'S':
74+
i += 1
75+
case 'E':
76+
j += 1
77+
case 'W':
78+
j -= 1
79+
if (i, j) in vis:
6980
return True
70-
vis.add((x, y))
81+
vis.add((i, j))
7182
return False
7283
```
7384

@@ -78,25 +89,20 @@ class Solution:
7889
```java
7990
class Solution {
8091
public boolean isPathCrossing(String path) {
81-
int x = 0;
82-
int y = 0;
92+
int i = 0, j = 0;
8393
Set<Integer> vis = new HashSet<>();
8494
vis.add(0);
85-
for (char c : path.toCharArray()) {
86-
if (c == 'N') {
87-
++y;
88-
} else if (c == 'S') {
89-
--y;
90-
} else if (c == 'E') {
91-
++x;
92-
} else {
93-
--x;
95+
for (int k = 0, n = path.length(); k < n; ++k) {
96+
switch (path.charAt(k)) {
97+
case 'N' -> --i;
98+
case 'S' -> ++i;
99+
case 'E' -> ++j;
100+
case 'W' -> --j;
94101
}
95-
int t = x * 20000 + y;
96-
if (vis.contains(t)) {
102+
int t = i * 20000 + j;
103+
if (!vis.add(t)) {
97104
return true;
98105
}
99-
vis.add(t);
100106
}
101107
return false;
102108
}
@@ -109,22 +115,25 @@ class Solution {
109115
class Solution {
110116
public:
111117
bool isPathCrossing(string path) {
112-
int x = 0, y = 0;
113-
unordered_set<int> vis {{0}};
114-
for (char c : path) {
115-
if (c == 'N')
116-
++y;
117-
else if (c == 'S')
118-
--y;
119-
else if (c == 'E')
120-
++x;
121-
else
122-
--x;
123-
int t = x * 20000 + y;
124-
if (vis.count(t)) return 1;
125-
vis.insert(t);
118+
int i = 0, j = 0;
119+
unordered_set<int> s{{0}};
120+
for (char& c : path) {
121+
if (c == 'N') {
122+
--i;
123+
} else if (c == 'S') {
124+
++i;
125+
} else if (c == 'E') {
126+
++j;
127+
} else {
128+
--j;
129+
}
130+
int t = i * 20000 + j;
131+
if (s.count(t)) {
132+
return true;
133+
}
134+
s.insert(t);
126135
}
127-
return 0;
136+
return false;
128137
}
129138
};
130139
```
@@ -133,24 +142,23 @@ public:
133142
134143
```go
135144
func isPathCrossing(path string) bool {
136-
x, y := 0, 0
137-
vis := make(map[int]bool)
138-
vis[0] = true
145+
i, j := 0, 0
146+
vis := map[int]bool{0: true}
139147
for _, c := range path {
140-
if c == 'N' {
141-
y++
142-
} else if c == 'S' {
143-
y--
144-
} else if c == 'E' {
145-
x++
146-
} else {
147-
x--
148+
switch c {
149+
case 'N':
150+
i--
151+
case 'S':
152+
i++
153+
case 'E':
154+
j++
155+
case 'W':
156+
j--
148157
}
149-
t := x*20000 + y
150-
if vis[t] {
158+
if vis[i*20000+j] {
151159
return true
152160
}
153-
vis[t] = true
161+
vis[i*20000+j] = true
154162
}
155163
return false
156164
}

solution/1400-1499/1496.Path Crossing/README_EN.md

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,21 @@
4141
```python
4242
class Solution:
4343
def isPathCrossing(self, path: str) -> bool:
44-
x = y = 0
45-
vis = {(x, y)}
44+
i = j = 0
45+
vis = {(0, 0)}
4646
for c in path:
47-
if c == 'N':
48-
y += 1
49-
elif c == 'S':
50-
y -= 1
51-
elif c == 'E':
52-
x += 1
53-
else:
54-
x -= 1
55-
if (x, y) in vis:
47+
match c:
48+
case 'N':
49+
i -= 1
50+
case 'S':
51+
i += 1
52+
case 'E':
53+
j += 1
54+
case 'W':
55+
j -= 1
56+
if (i, j) in vis:
5657
return True
57-
vis.add((x, y))
58+
vis.add((i, j))
5859
return False
5960
```
6061

@@ -63,25 +64,20 @@ class Solution:
6364
```java
6465
class Solution {
6566
public boolean isPathCrossing(String path) {
66-
int x = 0;
67-
int y = 0;
67+
int i = 0, j = 0;
6868
Set<Integer> vis = new HashSet<>();
6969
vis.add(0);
70-
for (char c : path.toCharArray()) {
71-
if (c == 'N') {
72-
++y;
73-
} else if (c == 'S') {
74-
--y;
75-
} else if (c == 'E') {
76-
++x;
77-
} else {
78-
--x;
70+
for (int k = 0, n = path.length(); k < n; ++k) {
71+
switch (path.charAt(k)) {
72+
case 'N' -> --i;
73+
case 'S' -> ++i;
74+
case 'E' -> ++j;
75+
case 'W' -> --j;
7976
}
80-
int t = x * 20000 + y;
81-
if (vis.contains(t)) {
77+
int t = i * 20000 + j;
78+
if (!vis.add(t)) {
8279
return true;
8380
}
84-
vis.add(t);
8581
}
8682
return false;
8783
}
@@ -94,22 +90,25 @@ class Solution {
9490
class Solution {
9591
public:
9692
bool isPathCrossing(string path) {
97-
int x = 0, y = 0;
98-
unordered_set<int> vis {{0}};
99-
for (char c : path) {
100-
if (c == 'N')
101-
++y;
102-
else if (c == 'S')
103-
--y;
104-
else if (c == 'E')
105-
++x;
106-
else
107-
--x;
108-
int t = x * 20000 + y;
109-
if (vis.count(t)) return 1;
110-
vis.insert(t);
93+
int i = 0, j = 0;
94+
unordered_set<int> s{{0}};
95+
for (char& c : path) {
96+
if (c == 'N') {
97+
--i;
98+
} else if (c == 'S') {
99+
++i;
100+
} else if (c == 'E') {
101+
++j;
102+
} else {
103+
--j;
104+
}
105+
int t = i * 20000 + j;
106+
if (s.count(t)) {
107+
return true;
108+
}
109+
s.insert(t);
111110
}
112-
return 0;
111+
return false;
113112
}
114113
};
115114
```
@@ -118,24 +117,23 @@ public:
118117
119118
```go
120119
func isPathCrossing(path string) bool {
121-
x, y := 0, 0
122-
vis := make(map[int]bool)
123-
vis[0] = true
120+
i, j := 0, 0
121+
vis := map[int]bool{0: true}
124122
for _, c := range path {
125-
if c == 'N' {
126-
y++
127-
} else if c == 'S' {
128-
y--
129-
} else if c == 'E' {
130-
x++
131-
} else {
132-
x--
123+
switch c {
124+
case 'N':
125+
i--
126+
case 'S':
127+
i++
128+
case 'E':
129+
j++
130+
case 'W':
131+
j--
133132
}
134-
t := x*20000 + y
135-
if vis[t] {
133+
if vis[i*20000+j] {
136134
return true
137135
}
138-
vis[t] = true
136+
vis[i*20000+j] = true
139137
}
140138
return false
141139
}
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
class Solution {
22
public:
33
bool isPathCrossing(string path) {
4-
int x = 0, y = 0;
5-
unordered_set<int> vis{{0}};
6-
for (char c : path) {
7-
if (c == 'N')
8-
++y;
9-
else if (c == 'S')
10-
--y;
11-
else if (c == 'E')
12-
++x;
13-
else
14-
--x;
15-
int t = x * 20000 + y;
16-
if (vis.count(t)) return 1;
17-
vis.insert(t);
4+
int i = 0, j = 0;
5+
unordered_set<int> s{{0}};
6+
for (char& c : path) {
7+
if (c == 'N') {
8+
--i;
9+
} else if (c == 'S') {
10+
++i;
11+
} else if (c == 'E') {
12+
++j;
13+
} else {
14+
--j;
15+
}
16+
int t = i * 20000 + j;
17+
if (s.count(t)) {
18+
return true;
19+
}
20+
s.insert(t);
1821
}
19-
return 0;
22+
return false;
2023
}
2124
};
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
func isPathCrossing(path string) bool {
2-
x, y := 0, 0
3-
vis := make(map[int]bool)
4-
vis[0] = true
2+
i, j := 0, 0
3+
vis := map[int]bool{0: true}
54
for _, c := range path {
6-
if c == 'N' {
7-
y++
8-
} else if c == 'S' {
9-
y--
10-
} else if c == 'E' {
11-
x++
12-
} else {
13-
x--
5+
switch c {
6+
case 'N':
7+
i--
8+
case 'S':
9+
i++
10+
case 'E':
11+
j++
12+
case 'W':
13+
j--
1414
}
15-
t := x*20000 + y
16-
if vis[t] {
15+
if vis[i*20000+j] {
1716
return true
1817
}
19-
vis[t] = true
18+
vis[i*20000+j] = true
2019
}
2120
return false
2221
}

0 commit comments

Comments
 (0)