Skip to content

Commit 5481598

Browse files
committed
feat: add solutions to leetcode problem: No.1497. Check If Array Pairs Are Divisible by k
1 parent 8a2c9f2 commit 5481598

File tree

5 files changed

+155
-4
lines changed

5 files changed

+155
-4
lines changed

solution/1400-1499/1497.Check If Array Pairs Are Divisible by k/README.md

+44-2
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,57 @@
7171
<!-- 这里可写当前语言的特殊实现逻辑 -->
7272

7373
```python
74-
74+
class Solution:
75+
def isPathCrossing(self, path: str) -> bool:
76+
x = y = 0
77+
visited = set()
78+
visited.add('0.0')
79+
for c in path:
80+
if c == 'N':
81+
y += 1
82+
elif c == 'S':
83+
y -= 1
84+
elif c == 'E':
85+
x += 1
86+
else:
87+
x -= 1
88+
pos = f'{x}.{y}'
89+
if pos in visited:
90+
return True
91+
visited.add(pos)
92+
return False
7593
```
7694

7795
### **Java**
7896

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

8199
```java
82-
100+
class Solution {
101+
public boolean isPathCrossing(String path) {
102+
Set<String> visited = new HashSet<>();
103+
visited.add("0.0");
104+
int x = 0, y = 0;
105+
for (int i = 0; i < path.length(); ++i) {
106+
char c = path.charAt(i);
107+
if (c == 'N') {
108+
++y;
109+
} else if (c == 'S') {
110+
--y;
111+
} else if (c == 'E') {
112+
++x;
113+
} else {
114+
--x;
115+
}
116+
String pos = x + "." + y;
117+
if (visited.contains(pos)) {
118+
return true;
119+
}
120+
visited.add(pos);
121+
}
122+
return false;
123+
}
124+
}
83125
```
84126

85127
### **...**

solution/1400-1499/1497.Check If Array Pairs Are Divisible by k/README_EN.md

+44-2
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,55 @@
6868
### **Python3**
6969

7070
```python
71-
71+
class Solution:
72+
def isPathCrossing(self, path: str) -> bool:
73+
x = y = 0
74+
visited = set()
75+
visited.add('0.0')
76+
for c in path:
77+
if c == 'N':
78+
y += 1
79+
elif c == 'S':
80+
y -= 1
81+
elif c == 'E':
82+
x += 1
83+
else:
84+
x -= 1
85+
pos = f'{x}.{y}'
86+
if pos in visited:
87+
return True
88+
visited.add(pos)
89+
return False
7290
```
7391

7492
### **Java**
7593

7694
```java
77-
95+
class Solution {
96+
public boolean isPathCrossing(String path) {
97+
Set<String> visited = new HashSet<>();
98+
visited.add("0.0");
99+
int x = 0, y = 0;
100+
for (int i = 0; i < path.length(); ++i) {
101+
char c = path.charAt(i);
102+
if (c == 'N') {
103+
++y;
104+
} else if (c == 'S') {
105+
--y;
106+
} else if (c == 'E') {
107+
++x;
108+
} else {
109+
--x;
110+
}
111+
String pos = x + "." + y;
112+
if (visited.contains(pos)) {
113+
return true;
114+
}
115+
visited.add(pos);
116+
}
117+
return false;
118+
}
119+
}
78120
```
79121

80122
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public boolean isPathCrossing(String path) {
3+
Set<String> visited = new HashSet<>();
4+
visited.add("0.0");
5+
int x = 0, y = 0;
6+
for (int i = 0; i < path.length(); ++i) {
7+
char c = path.charAt(i);
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+
String pos = x + "." + y;
18+
if (visited.contains(pos)) {
19+
return true;
20+
}
21+
visited.add(pos);
22+
}
23+
return false;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def isPathCrossing(self, path: str) -> bool:
3+
x = y = 0
4+
visited = set()
5+
visited.add('0.0')
6+
for c in path:
7+
if c == 'N':
8+
y += 1
9+
elif c == 'S':
10+
y -= 1
11+
elif c == 'E':
12+
x += 1
13+
else:
14+
x -= 1
15+
pos = f'{x}.{y}'
16+
if pos in visited:
17+
return True
18+
visited.add(pos)
19+
return False

test.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def isPathCrossing(self, path: str) -> bool:
3+
x = y = 0
4+
visited = set()
5+
visited.add(f'{x}.{y}')
6+
for c in path:
7+
if c == 'N':
8+
y += 1
9+
elif c == 'S':
10+
y -= 1
11+
elif c == 'E':
12+
x += 1
13+
else:
14+
x -= 1
15+
pos = f'{x}.{y}'
16+
if pos in visited:
17+
return True
18+
visited.add(pos)
19+
return False
20+
21+
22+
if __name__ == '__main__':
23+
print(Solution().isPathCrossing("NESWW"))

0 commit comments

Comments
 (0)