Skip to content

Commit

Permalink
Merge pull request #767 from youngqqcn/master
Browse files Browse the repository at this point in the history
0841.钥匙和房间.md, 增加 Golang, Java, Python 实现
  • Loading branch information
youngyangyang04 authored Sep 21, 2021
2 parents fb2b6c6 + 0bb07f3 commit 2ecd4ac
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 5 deletions.
99 changes: 99 additions & 0 deletions problems/0841.钥匙和房间.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,109 @@ public:

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:

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

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:

-----------------------
Expand Down
41 changes: 41 additions & 0 deletions problems/0844.比较含退格的字符串.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,49 @@ class Solution {

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

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:

-----------------------
Expand Down
28 changes: 26 additions & 2 deletions problems/0922.按奇偶排序数组II.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

# 922. 按奇偶排序数组II

[力扣题目链接](https://leetcode-cn.com/problems/sort-array-by-parity-ii/)

给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。

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

## Python
## Python3

```python3
```python
#方法2
class Solution:
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
Expand Down Expand Up @@ -180,6 +182,28 @@ class Solution:
## 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
Expand Down
26 changes: 25 additions & 1 deletion problems/0925.长按键入.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Python:
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
i, j = 0, 0
m, n = len(name) , len(typed)
m, n = len(name) , len(typed)
while i< m and j < n:
if name[i] == typed[j]: # 相同时向后匹配
i += 1
Expand All @@ -155,8 +155,32 @@ class Solution:
else: return False
return True
```

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:

-----------------------
Expand Down
42 changes: 40 additions & 2 deletions problems/0941.有效的山脉数组.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</p>
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>

# 941.有效的山脉数组
# 941.有效的山脉数组

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

Expand Down Expand Up @@ -103,14 +103,52 @@ class Solution {
}
```

## Python
## Python3

```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
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
Expand Down

0 comments on commit 2ecd4ac

Please sign in to comment.