Skip to content

Commit 1c4b51d

Browse files
committed
feat: add solutions to lc problem: No.1306
No.1306.Jump Game III
1 parent ec20628 commit 1c4b51d

File tree

10 files changed

+246
-11
lines changed

10 files changed

+246
-11
lines changed

solution/1300-1399/1306.Jump Game III/README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,103 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
BFS。
58+
5759
<!-- tabs:start -->
5860

5961
### **Python3**
6062

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

6365
```python
64-
66+
class Solution:
67+
def canReach(self, arr: List[int], start: int) -> bool:
68+
n = len(arr)
69+
q = deque([start])
70+
while q:
71+
i = q.popleft()
72+
if arr[i] == 0:
73+
return True
74+
for j in [i + arr[i], i - arr[i]]:
75+
if 0 <= j < n and arr[j] >= 0:
76+
q.append(j)
77+
arr[i] = -1
78+
return False
6579
```
6680

6781
### **Java**
6882

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

7185
```java
86+
class Solution {
87+
public boolean canReach(int[] arr, int start) {
88+
int n = arr.length;
89+
Deque<Integer> q = new ArrayDeque<>();
90+
q.offer(start);
91+
while (!q.isEmpty()) {
92+
int i = q.poll();
93+
if (arr[i] == 0) {
94+
return true;
95+
}
96+
for (int j : Arrays.asList(i + arr[i], i - arr[i])) {
97+
if (j >= 0 && j < n && arr[j] >= 0) {
98+
q.offer(j);
99+
}
100+
}
101+
arr[i] = -1;
102+
}
103+
return false;
104+
}
105+
}
106+
```
107+
108+
### **C++**
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
bool canReach(vector<int> &arr, int start) {
114+
int n = arr.size();
115+
queue<int> q{{start}};
116+
while (!q.empty())
117+
{
118+
int i = q.front();
119+
if (arr[i] == 0)
120+
return 1;
121+
q.pop();
122+
for (int j : {i + arr[i], i - arr[i]})
123+
{
124+
if (j >= 0 && j < n && arr[j] >= 0)
125+
q.push(j);
126+
}
127+
arr[i] = -1;
128+
}
129+
return 0;
130+
}
131+
};
132+
```
72133
134+
### **Go**
135+
136+
```go
137+
func canReach(arr []int, start int) bool {
138+
q := []int{start}
139+
for len(q) > 0 {
140+
i := q[0]
141+
if arr[i] == 0 {
142+
return true
143+
}
144+
q = q[1:]
145+
for _, j := range []int{i + arr[i], i - arr[i]} {
146+
if j >= 0 && j < len(arr) && arr[j] >= 0 {
147+
q = append(q, j)
148+
}
149+
}
150+
arr[i] = -1
151+
}
152+
return false
153+
}
73154
```
74155

75156
### **...**

solution/1300-1399/1306.Jump Game III/README_EN.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,99 @@ index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3
4949

5050
## Solutions
5151

52+
BFS.
53+
5254
<!-- tabs:start -->
5355

5456
### **Python3**
5557

5658
```python
57-
59+
class Solution:
60+
def canReach(self, arr: List[int], start: int) -> bool:
61+
n = len(arr)
62+
q = deque([start])
63+
while q:
64+
i = q.popleft()
65+
if arr[i] == 0:
66+
return True
67+
for j in [i + arr[i], i - arr[i]]:
68+
if 0 <= j < n and arr[j] >= 0:
69+
q.append(j)
70+
arr[i] = -1
71+
return False
5872
```
5973

6074
### **Java**
6175

6276
```java
77+
class Solution {
78+
public boolean canReach(int[] arr, int start) {
79+
int n = arr.length;
80+
Deque<Integer> q = new ArrayDeque<>();
81+
q.offer(start);
82+
while (!q.isEmpty()) {
83+
int i = q.poll();
84+
if (arr[i] == 0) {
85+
return true;
86+
}
87+
for (int j : Arrays.asList(i + arr[i], i - arr[i])) {
88+
if (j >= 0 && j < n && arr[j] >= 0) {
89+
q.offer(j);
90+
}
91+
}
92+
arr[i] = -1;
93+
}
94+
return false;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
bool canReach(vector<int> &arr, int start) {
105+
int n = arr.size();
106+
queue<int> q{{start}};
107+
while (!q.empty())
108+
{
109+
int i = q.front();
110+
if (arr[i] == 0)
111+
return 1;
112+
q.pop();
113+
for (int j : {i + arr[i], i - arr[i]})
114+
{
115+
if (j >= 0 && j < n && arr[j] >= 0)
116+
q.push(j);
117+
}
118+
arr[i] = -1;
119+
}
120+
return 0;
121+
}
122+
};
123+
```
63124
125+
### **Go**
126+
127+
```go
128+
func canReach(arr []int, start int) bool {
129+
q := []int{start}
130+
for len(q) > 0 {
131+
i := q[0]
132+
if arr[i] == 0 {
133+
return true
134+
}
135+
q = q[1:]
136+
for _, j := range []int{i + arr[i], i - arr[i]} {
137+
if j >= 0 && j < len(arr) && arr[j] >= 0 {
138+
q = append(q, j)
139+
}
140+
}
141+
arr[i] = -1
142+
}
143+
return false
144+
}
64145
```
65146

66147
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
bool canReach(vector<int> &arr, int start) {
4+
int n = arr.size();
5+
queue<int> q{{start}};
6+
while (!q.empty())
7+
{
8+
int i = q.front();
9+
if (arr[i] == 0)
10+
return 1;
11+
q.pop();
12+
for (int j : {i + arr[i], i - arr[i]})
13+
{
14+
if (j >= 0 && j < n && arr[j] >= 0)
15+
q.push(j);
16+
}
17+
arr[i] = -1;
18+
}
19+
return 0;
20+
}
21+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func canReach(arr []int, start int) bool {
2+
q := []int{start}
3+
for len(q) > 0 {
4+
i := q[0]
5+
if arr[i] == 0 {
6+
return true
7+
}
8+
q = q[1:]
9+
for _, j := range []int{i + arr[i], i - arr[i]} {
10+
if j >= 0 && j < len(arr) && arr[j] >= 0 {
11+
q = append(q, j)
12+
}
13+
}
14+
arr[i] = -1
15+
}
16+
return false
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public boolean canReach(int[] arr, int start) {
3+
int n = arr.length;
4+
Deque<Integer> q = new ArrayDeque<>();
5+
q.offer(start);
6+
while (!q.isEmpty()) {
7+
int i = q.poll();
8+
if (arr[i] == 0) {
9+
return true;
10+
}
11+
for (int j : Arrays.asList(i + arr[i], i - arr[i])) {
12+
if (j >= 0 && j < n && arr[j] >= 0) {
13+
q.offer(j);
14+
}
15+
}
16+
arr[i] = -1;
17+
}
18+
return false;
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def canReach(self, arr: List[int], start: int) -> bool:
3+
n = len(arr)
4+
q = deque([start])
5+
while q:
6+
i = q.popleft()
7+
if arr[i] == 0:
8+
return True
9+
for j in [i + arr[i], i - arr[i]]:
10+
if 0 <= j < n and arr[j] >= 0:
11+
q.append(j)
12+
arr[i] = -1
13+
return False

solution/1700-1799/1791.Find Center of Star Graph/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@
5353
```python
5454
class Solution:
5555
def findCenter(self, edges: List[List[int]]) -> int:
56-
a, b = edges[0]
57-
c, d = edges[1]
58-
return a if a == c or a == d else b
56+
return edges[0][0] if edges[0][0] in edges[1] else edges[0][1]
5957
```
6058

6159
### **Java**

solution/1700-1799/1791.Find Center of Star Graph/README_EN.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@
5656
```python
5757
class Solution:
5858
def findCenter(self, edges: List[List[int]]) -> int:
59-
a, b = edges[0]
60-
c, d = edges[1]
61-
return a if a == c or a == d else b
59+
return edges[0][0] if edges[0][0] in edges[1] else edges[0][1]
6260
```
6361

6462
### **Java**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
int findCenter(vector<vector<int>>& edges) {
4+
int a = edges[0][0], b = edges[0][1];
5+
int c = edges[1][0], d = edges[1][1];
6+
return a == c || a == d ? a : b;
7+
}
8+
};
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
class Solution:
22
def findCenter(self, edges: List[List[int]]) -> int:
3-
a, b = edges[0]
4-
c, d = edges[1]
5-
return a if a == c or a == d else b
3+
return edges[0][0] if edges[0][0] in edges[1] else edges[0][1]

0 commit comments

Comments
 (0)