mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #767 from youngqqcn/master
0841.钥匙和房间.md, 增加 Golang, Java, Python 实现
This commit is contained in:
@ -121,10 +121,109 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
private void dfs(int key, List<List<Integer>> rooms, List<Boolean> visited) {
|
||||||
|
if (visited.get(key)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
visited.set(key, true);
|
||||||
|
for (int k : rooms.get(key)) {
|
||||||
|
// 深度优先搜索遍历
|
||||||
|
dfs(k, rooms, visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
|
||||||
|
List<Boolean> visited = new ArrayList<Boolean>(){{
|
||||||
|
for(int i = 0 ; i < rooms.size(); i++){
|
||||||
|
add(false);
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
|
||||||
|
dfs(0, rooms, visited);
|
||||||
|
|
||||||
|
//检查是否都访问到了
|
||||||
|
for (boolean flag : visited) {
|
||||||
|
if (!flag) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
python3
|
||||||
|
|
||||||
|
```python
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
|
||||||
|
def dfs(self, key: int, rooms: List[List[int]] , visited : List[bool] ) :
|
||||||
|
if visited[key] :
|
||||||
|
return
|
||||||
|
|
||||||
|
visited[key] = True
|
||||||
|
keys = rooms[key]
|
||||||
|
for i in range(len(keys)) :
|
||||||
|
# 深度优先搜索遍历
|
||||||
|
self.dfs(keys[i], rooms, visited)
|
||||||
|
|
||||||
|
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
|
||||||
|
visited = [False for i in range(len(rooms))]
|
||||||
|
|
||||||
|
self.dfs(0, rooms, visited)
|
||||||
|
|
||||||
|
# 检查是否都访问到了
|
||||||
|
for i in range(len(visited)):
|
||||||
|
if not visited[i] :
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
```go
|
||||||
|
|
||||||
|
func dfs(key int, rooms [][]int, visited []bool ) {
|
||||||
|
if visited[key] {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
visited[key] = true
|
||||||
|
keys := rooms[key]
|
||||||
|
for _ , key := range keys {
|
||||||
|
// 深度优先搜索遍历
|
||||||
|
dfs(key, rooms, visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func canVisitAllRooms(rooms [][]int) bool {
|
||||||
|
|
||||||
|
visited := make([]bool, len(rooms));
|
||||||
|
|
||||||
|
dfs(0, rooms, visited);
|
||||||
|
|
||||||
|
//检查是否都访问到了
|
||||||
|
for i := 0; i < len(visited); i++ {
|
||||||
|
if !visited[i] {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
@ -188,8 +188,49 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
python3
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
|
||||||
|
def get_string(self, s: str) -> str :
|
||||||
|
bz = []
|
||||||
|
for i in range(len(s)) :
|
||||||
|
c = s[i]
|
||||||
|
if c != '#' :
|
||||||
|
bz.append(c) # 模拟入栈
|
||||||
|
elif len(bz) > 0: # 栈非空才能弹栈
|
||||||
|
bz.pop() # 模拟弹栈
|
||||||
|
return str(bz)
|
||||||
|
|
||||||
|
def backspaceCompare(self, s: str, t: str) -> bool:
|
||||||
|
return self.get_string(s) == self.get_string(t)
|
||||||
|
pass
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
```go
|
||||||
|
|
||||||
|
func getString(s string) string {
|
||||||
|
bz := []rune{}
|
||||||
|
for _, c := range s {
|
||||||
|
if c != '#' {
|
||||||
|
bz = append(bz, c); // 模拟入栈
|
||||||
|
} else if len(bz) > 0 { // 栈非空才能弹栈
|
||||||
|
bz = bz[:len(bz)-1] // 模拟弹栈
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(bz)
|
||||||
|
}
|
||||||
|
|
||||||
|
func backspaceCompare(s string, t string) bool {
|
||||||
|
return getString(s) == getString(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
# 922. 按奇偶排序数组II
|
# 922. 按奇偶排序数组II
|
||||||
|
|
||||||
|
[力扣题目链接](https://leetcode-cn.com/problems/sort-array-by-parity-ii/)
|
||||||
|
|
||||||
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。
|
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。
|
||||||
|
|
||||||
对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。
|
对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。
|
||||||
@ -147,9 +149,9 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Python
|
## Python3
|
||||||
|
|
||||||
```python3
|
```python
|
||||||
#方法2
|
#方法2
|
||||||
class Solution:
|
class Solution:
|
||||||
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
|
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
|
||||||
@ -180,6 +182,28 @@ class Solution:
|
|||||||
## Go
|
## Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
||||||
|
// 方法一
|
||||||
|
func sortArrayByParityII(nums []int) []int {
|
||||||
|
// 分别存放 nums 中的奇数、偶数
|
||||||
|
even, odd := []int{}, []int{}
|
||||||
|
for i := 0; i < len(nums); i++ {
|
||||||
|
if (nums[i] % 2 == 0) {
|
||||||
|
even = append(even, nums[i])
|
||||||
|
} else {
|
||||||
|
odd = append(odd, nums[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 把奇偶数组重新存回 nums
|
||||||
|
result := make([]int, len(nums))
|
||||||
|
index := 0
|
||||||
|
for i := 0; i < len(even); i++ {
|
||||||
|
result[index] = even[i]; index++;
|
||||||
|
result[index] = odd[i]; index++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## JavaScript
|
## JavaScript
|
||||||
|
@ -155,8 +155,32 @@ class Solution:
|
|||||||
else: return False
|
else: return False
|
||||||
return True
|
return True
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
```go
|
||||||
|
|
||||||
|
func isLongPressedName(name string, typed string) bool {
|
||||||
|
if(name[0] != typed[0] || len(name) > len(typed)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := 0 // name的索引
|
||||||
|
var last byte // 上个匹配字符
|
||||||
|
for i := 0; i < len(typed); i++ {
|
||||||
|
if idx < len(name) && name[idx] == typed[i] {
|
||||||
|
last = name[idx]
|
||||||
|
idx++
|
||||||
|
} else if last == typed[i] {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return idx == len(name)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
@ -103,14 +103,52 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Python
|
## Python3
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
class Solution:
|
||||||
|
def validMountainArray(self, arr: List[int]) -> bool:
|
||||||
|
if len(arr) < 3 :
|
||||||
|
return False
|
||||||
|
|
||||||
|
i = 1
|
||||||
|
flagIncrease = False # 上升
|
||||||
|
flagDecrease = False # 下降
|
||||||
|
|
||||||
|
while i < len(arr) and arr[i-1] < arr[i]:
|
||||||
|
flagIncrease = True
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
while i < len(arr) and arr[i-1] > arr[i]:
|
||||||
|
flagDecrease = True
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
return i == len(arr) and flagIncrease and flagDecrease
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Go
|
## Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
func validMountainArray(arr []int) bool {
|
||||||
|
if len(arr) < 3 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
i := 1
|
||||||
|
flagIncrease := false // 上升
|
||||||
|
flagDecrease := false // 下降
|
||||||
|
|
||||||
|
for ; i < len(arr) && arr[i-1] < arr[i]; i++ {
|
||||||
|
flagIncrease = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ; i < len(arr) && arr[i-1] > arr[i]; i++ {
|
||||||
|
flagDecrease = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return i == len(arr) && flagIncrease && flagDecrease;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## JavaScript
|
## JavaScript
|
||||||
|
Reference in New Issue
Block a user