Skip to content

Commit a4dd957

Browse files
committed
feat: add solutions to lc problems: No.1496,1497
1 parent c4242fa commit a4dd957

File tree

12 files changed

+400
-116
lines changed

12 files changed

+400
-116
lines changed

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

+91-1
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,105 @@
5050
<!-- 这里可写当前语言的特殊实现逻辑 -->
5151

5252
```python
53-
53+
class Solution:
54+
def isPathCrossing(self, path: str) -> bool:
55+
x = y = 0
56+
vis = set([(x, y)])
57+
for c in path:
58+
if c == 'N':
59+
y += 1
60+
elif c == 'S':
61+
y -= 1
62+
elif c == 'E':
63+
x += 1
64+
else:
65+
x -= 1
66+
if (x, y) in vis:
67+
return True
68+
vis.add((x, y))
69+
return False
5470
```
5571

5672
### **Java**
5773

5874
<!-- 这里可写当前语言的特殊实现逻辑 -->
5975

6076
```java
77+
class Solution {
78+
public boolean isPathCrossing(String path) {
79+
int x = 0;
80+
int y = 0;
81+
Set<Integer> vis = new HashSet<>();
82+
vis.add(0);
83+
for (char c : path.toCharArray()) {
84+
if (c == 'N') {
85+
++y;
86+
} else if (c == 'S') {
87+
--y;
88+
} else if (c == 'E') {
89+
++x;
90+
} else {
91+
--x;
92+
}
93+
int t = x * 20000 + y;
94+
if (vis.contains(t)) {
95+
return true;
96+
}
97+
vis.add(t);
98+
}
99+
return false;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
bool isPathCrossing(string path) {
110+
int x = 0, y = 0;
111+
unordered_set<int> vis{{0}};
112+
for (char c : path)
113+
{
114+
if (c == 'N') ++y;
115+
else if (c == 'S') --y;
116+
else if (c == 'E') ++x;
117+
else --x;
118+
int t = x * 20000 + y;
119+
if (vis.count(t)) return 1;
120+
vis.insert(t);
121+
}
122+
return 0;
123+
}
124+
};
125+
```
61126
127+
### **Go**
128+
129+
```go
130+
func isPathCrossing(path string) bool {
131+
x, y := 0, 0
132+
vis := make(map[int]bool)
133+
vis[0] = true
134+
for _, c := range path {
135+
if c == 'N' {
136+
y++
137+
} else if c == 'S' {
138+
y--
139+
} else if c == 'E' {
140+
x++
141+
} else {
142+
x--
143+
}
144+
t := x*20000 + y
145+
if vis[t] {
146+
return true
147+
}
148+
vis[t] = true
149+
}
150+
return false
151+
}
62152
```
63153

64154
### **...**

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

+91-1
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,103 @@
4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def isPathCrossing(self, path: str) -> bool:
48+
x = y = 0
49+
vis = set([(x, y)])
50+
for c in path:
51+
if c == 'N':
52+
y += 1
53+
elif c == 'S':
54+
y -= 1
55+
elif c == 'E':
56+
x += 1
57+
else:
58+
x -= 1
59+
if (x, y) in vis:
60+
return True
61+
vis.add((x, y))
62+
return False
4763
```
4864

4965
### **Java**
5066

5167
```java
68+
class Solution {
69+
public boolean isPathCrossing(String path) {
70+
int x = 0;
71+
int y = 0;
72+
Set<Integer> vis = new HashSet<>();
73+
vis.add(0);
74+
for (char c : path.toCharArray()) {
75+
if (c == 'N') {
76+
++y;
77+
} else if (c == 'S') {
78+
--y;
79+
} else if (c == 'E') {
80+
++x;
81+
} else {
82+
--x;
83+
}
84+
int t = x * 20000 + y;
85+
if (vis.contains(t)) {
86+
return true;
87+
}
88+
vis.add(t);
89+
}
90+
return false;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
bool isPathCrossing(string path) {
101+
int x = 0, y = 0;
102+
unordered_set<int> vis{{0}};
103+
for (char c : path)
104+
{
105+
if (c == 'N') ++y;
106+
else if (c == 'S') --y;
107+
else if (c == 'E') ++x;
108+
else --x;
109+
int t = x * 20000 + y;
110+
if (vis.count(t)) return 1;
111+
vis.insert(t);
112+
}
113+
return 0;
114+
}
115+
};
116+
```
52117
118+
### **Go**
119+
120+
```go
121+
func isPathCrossing(path string) bool {
122+
x, y := 0, 0
123+
vis := make(map[int]bool)
124+
vis[0] = true
125+
for _, c := range path {
126+
if c == 'N' {
127+
y++
128+
} else if c == 'S' {
129+
y--
130+
} else if c == 'E' {
131+
x++
132+
} else {
133+
x--
134+
}
135+
t := x*20000 + y
136+
if vis[t] {
137+
return true
138+
}
139+
vis[t] = true
140+
}
141+
return false
142+
}
53143
```
54144

55145
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
bool isPathCrossing(string path) {
4+
int x = 0, y = 0;
5+
unordered_set<int> vis{{0}};
6+
for (char c : path)
7+
{
8+
if (c == 'N') ++y;
9+
else if (c == 'S') --y;
10+
else if (c == 'E') ++x;
11+
else --x;
12+
int t = x * 20000 + y;
13+
if (vis.count(t)) return 1;
14+
vis.insert(t);
15+
}
16+
return 0;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func isPathCrossing(path string) bool {
2+
x, y := 0, 0
3+
vis := make(map[int]bool)
4+
vis[0] = true
5+
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--
14+
}
15+
t := x*20000 + y
16+
if vis[t] {
17+
return true
18+
}
19+
vis[t] = true
20+
}
21+
return false
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public boolean isPathCrossing(String path) {
3+
int x = 0;
4+
int y = 0;
5+
Set<Integer> vis = new HashSet<>();
6+
vis.add(0);
7+
for (char c : path.toCharArray()) {
8+
if (c == 'N') {
9+
++y;
10+
} else if (c == 'S') {
11+
--y;
12+
} else if (c == 'E') {
13+
++x;
14+
} else {
15+
--x;
16+
}
17+
int t = x * 20000 + y;
18+
if (vis.contains(t)) {
19+
return true;
20+
}
21+
vis.add(t);
22+
}
23+
return false;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def isPathCrossing(self, path: str) -> bool:
3+
x = y = 0
4+
vis = set([(x, y)])
5+
for c in path:
6+
if c == 'N':
7+
y += 1
8+
elif c == 'S':
9+
y -= 1
10+
elif c == 'E':
11+
x += 1
12+
else:
13+
x -= 1
14+
if (x, y) in vis:
15+
return True
16+
vis.add((x, y))
17+
return False

0 commit comments

Comments
 (0)