Skip to content

Commit 2ecd4ac

Browse files
Merge pull request youngyangyang04#767 from youngqqcn/master
0841.钥匙和房间.md, 增加 Golang, Java, Python 实现
2 parents fb2b6c6 + 0bb07f3 commit 2ecd4ac

File tree

5 files changed

+231
-5
lines changed

5 files changed

+231
-5
lines changed

problems/0841.钥匙和房间.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,109 @@ public:
121121

122122
Java:
123123

124+
```java
125+
class Solution {
126+
private void dfs(int key, List<List<Integer>> rooms, List<Boolean> visited) {
127+
if (visited.get(key)) {
128+
return;
129+
}
130+
131+
visited.set(key, true);
132+
for (int k : rooms.get(key)) {
133+
// 深度优先搜索遍历
134+
dfs(k, rooms, visited);
135+
}
136+
}
137+
138+
139+
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
140+
List<Boolean> visited = new ArrayList<Boolean>(){{
141+
for(int i = 0 ; i < rooms.size(); i++){
142+
add(false);
143+
}
144+
}};
145+
146+
dfs(0, rooms, visited);
147+
148+
//检查是否都访问到了
149+
for (boolean flag : visited) {
150+
if (!flag) {
151+
return false;
152+
}
153+
}
154+
return true;
155+
}
156+
}
157+
```
158+
159+
160+
161+
124162
Python:
125163

164+
python3
165+
166+
```python
167+
168+
class Solution:
169+
170+
def dfs(self, key: int, rooms: List[List[int]] , visited : List[bool] ) :
171+
if visited[key] :
172+
return
173+
174+
visited[key] = True
175+
keys = rooms[key]
176+
for i in range(len(keys)) :
177+
# 深度优先搜索遍历
178+
self.dfs(keys[i], rooms, visited)
179+
180+
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
181+
visited = [False for i in range(len(rooms))]
182+
183+
self.dfs(0, rooms, visited)
184+
185+
# 检查是否都访问到了
186+
for i in range(len(visited)):
187+
if not visited[i] :
188+
return False
189+
return True
190+
191+
```
192+
193+
126194
Go:
127195

196+
```go
197+
198+
func dfs(key int, rooms [][]int, visited []bool ) {
199+
if visited[key] {
200+
return;
201+
}
202+
203+
visited[key] = true
204+
keys := rooms[key]
205+
for _ , key := range keys {
206+
// 深度优先搜索遍历
207+
dfs(key, rooms, visited);
208+
}
209+
}
210+
211+
func canVisitAllRooms(rooms [][]int) bool {
212+
213+
visited := make([]bool, len(rooms));
214+
215+
dfs(0, rooms, visited);
216+
217+
//检查是否都访问到了
218+
for i := 0; i < len(visited); i++ {
219+
if !visited[i] {
220+
return false;
221+
}
222+
}
223+
return true;
224+
}
225+
```
226+
128227
JavaScript:
129228

130229
-----------------------

problems/0844.比较含退格的字符串.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,49 @@ class Solution {
188188

189189
Python:
190190

191+
python3
192+
193+
```python
194+
class Solution:
195+
196+
def get_string(self, s: str) -> str :
197+
bz = []
198+
for i in range(len(s)) :
199+
c = s[i]
200+
if c != '#' :
201+
bz.append(c) # 模拟入栈
202+
elif len(bz) > 0: # 栈非空才能弹栈
203+
bz.pop() # 模拟弹栈
204+
return str(bz)
205+
206+
def backspaceCompare(self, s: str, t: str) -> bool:
207+
return self.get_string(s) == self.get_string(t)
208+
pass
209+
```
210+
211+
191212
Go:
192213

214+
```go
215+
216+
func getString(s string) string {
217+
bz := []rune{}
218+
for _, c := range s {
219+
if c != '#' {
220+
bz = append(bz, c); // 模拟入栈
221+
} else if len(bz) > 0 { // 栈非空才能弹栈
222+
bz = bz[:len(bz)-1] // 模拟弹栈
223+
}
224+
}
225+
return string(bz)
226+
}
227+
228+
func backspaceCompare(s string, t string) bool {
229+
return getString(s) == getString(t)
230+
}
231+
232+
```
233+
193234
JavaScript:
194235

195236
-----------------------

problems/0922.按奇偶排序数组II.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
# 922. 按奇偶排序数组II
1313

14+
[力扣题目链接](https://leetcode-cn.com/problems/sort-array-by-parity-ii/)
15+
1416
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。
1517

1618
对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。
@@ -147,9 +149,9 @@ class Solution {
147149
}
148150
```
149151

150-
## Python
152+
## Python3
151153

152-
```python3
154+
```python
153155
#方法2
154156
class Solution:
155157
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
@@ -180,6 +182,28 @@ class Solution:
180182
## Go
181183

182184
```go
185+
186+
// 方法一
187+
func sortArrayByParityII(nums []int) []int {
188+
// 分别存放 nums 中的奇数、偶数
189+
even, odd := []int{}, []int{}
190+
for i := 0; i < len(nums); i++ {
191+
if (nums[i] % 2 == 0) {
192+
even = append(even, nums[i])
193+
} else {
194+
odd = append(odd, nums[i])
195+
}
196+
}
197+
198+
// 把奇偶数组重新存回 nums
199+
result := make([]int, len(nums))
200+
index := 0
201+
for i := 0; i < len(even); i++ {
202+
result[index] = even[i]; index++;
203+
result[index] = odd[i]; index++;
204+
}
205+
return result;
206+
}
183207
```
184208

185209
## JavaScript

problems/0925.长按键入.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Python:
134134
class Solution:
135135
def isLongPressedName(self, name: str, typed: str) -> bool:
136136
i, j = 0, 0
137-
m, n = len(name) , len(typed)
137+
m, n = len(name) , len(typed)
138138
while i< m and j < n:
139139
if name[i] == typed[j]: # 相同时向后匹配
140140
i += 1
@@ -155,8 +155,32 @@ class Solution:
155155
else: return False
156156
return True
157157
```
158+
158159
Go:
159160

161+
```go
162+
163+
func isLongPressedName(name string, typed string) bool {
164+
if(name[0] != typed[0] || len(name) > len(typed)) {
165+
return false;
166+
}
167+
168+
idx := 0 // name的索引
169+
var last byte // 上个匹配字符
170+
for i := 0; i < len(typed); i++ {
171+
if idx < len(name) && name[idx] == typed[i] {
172+
last = name[idx]
173+
idx++
174+
} else if last == typed[i] {
175+
continue
176+
} else {
177+
return false
178+
}
179+
}
180+
return idx == len(name)
181+
}
182+
```
183+
160184
JavaScript:
161185

162186
-----------------------

problems/0941.有效的山脉数组.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</p>
88
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
99

10-
# 941.有效的山脉数组
10+
# 941.有效的山脉数组
1111

1212
[力扣题目链接](https://leetcode-cn.com/problems/valid-mountain-array/)
1313

@@ -103,14 +103,52 @@ class Solution {
103103
}
104104
```
105105

106-
## Python
106+
## Python3
107107

108108
```python
109+
class Solution:
110+
def validMountainArray(self, arr: List[int]) -> bool:
111+
if len(arr) < 3 :
112+
return False
113+
114+
i = 1
115+
flagIncrease = False # 上升
116+
flagDecrease = False # 下降
117+
118+
while i < len(arr) and arr[i-1] < arr[i]:
119+
flagIncrease = True
120+
i += 1
121+
122+
while i < len(arr) and arr[i-1] > arr[i]:
123+
flagDecrease = True
124+
i += 1
125+
126+
return i == len(arr) and flagIncrease and flagDecrease
127+
109128
```
110129

111130
## Go
112131

113132
```go
133+
func validMountainArray(arr []int) bool {
134+
if len(arr) < 3 {
135+
return false
136+
}
137+
138+
i := 1
139+
flagIncrease := false // 上升
140+
flagDecrease := false // 下降
141+
142+
for ; i < len(arr) && arr[i-1] < arr[i]; i++ {
143+
flagIncrease = true;
144+
}
145+
146+
for ; i < len(arr) && arr[i-1] > arr[i]; i++ {
147+
flagDecrease = true;
148+
}
149+
150+
return i == len(arr) && flagIncrease && flagDecrease;
151+
}
114152
```
115153

116154
## JavaScript

0 commit comments

Comments
 (0)