From 2b1f90a266733882a0b06eb1a846297a65f20c89 Mon Sep 17 00:00:00 2001 From: yqq Date: Sat, 18 Sep 2021 16:17:11 +0800 Subject: [PATCH 1/5] =?UTF-8?q?0841.=E9=92=A5=E5=8C=99=E5=92=8C=E6=88=BF?= =?UTF-8?q?=E9=97=B4.md,=20=E5=A2=9E=E5=8A=A0=20Golang,=20Java,=20Python?= =?UTF-8?q?=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0841.钥匙和房间.md | 99 ++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/problems/0841.钥匙和房间.md b/problems/0841.钥匙和房间.md index 4a0185ec..2ce9d343 100644 --- a/problems/0841.钥匙和房间.md +++ b/problems/0841.钥匙和房间.md @@ -121,10 +121,109 @@ public: Java: +```java +class Solution { + private void dfs(int key, List> rooms, List visited) { + if (visited.get(key)) { + return; + } + + visited.set(key, true); + for (int k : rooms.get(key)) { + // 深度优先搜索遍历 + dfs(k, rooms, visited); + } + } + + + public boolean canVisitAllRooms(List> rooms) { + List visited = new ArrayList(){{ + 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: ----------------------- From 9b37c68aaffd2d789ff2161520db3465016a495a Mon Sep 17 00:00:00 2001 From: yqq Date: Sat, 18 Sep 2021 16:55:21 +0800 Subject: [PATCH 2/5] =?UTF-8?q?0844.=E6=AF=94=E8=BE=83=E5=90=AB=E9=80=80?= =?UTF-8?q?=E6=A0=BC=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2.md,=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0Python,=20Golang=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0841.钥匙和房间.md | 2 +- problems/0844.比较含退格的字符串.md | 41 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/problems/0841.钥匙和房间.md b/problems/0841.钥匙和房间.md index 2ce9d343..caa0edaf 100644 --- a/problems/0841.钥匙和房间.md +++ b/problems/0841.钥匙和房间.md @@ -212,7 +212,7 @@ func canVisitAllRooms(rooms [][]int) bool { visited := make([]bool, len(rooms)); - dfs(0, rooms, visited); + dfs(0, rooms, visited); //检查是否都访问到了 for i := 0; i < len(visited); i++ { diff --git a/problems/0844.比较含退格的字符串.md b/problems/0844.比较含退格的字符串.md index e6bf3493..74455ace 100644 --- a/problems/0844.比较含退格的字符串.md +++ b/problems/0844.比较含退格的字符串.md @@ -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: ----------------------- From d88ba0549bc38f2e577d13a18d8bb606529426b1 Mon Sep 17 00:00:00 2001 From: yqq Date: Sun, 19 Sep 2021 10:59:16 +0800 Subject: [PATCH 3/5] =?UTF-8?q?0922.=E6=8C=89=E5=A5=87=E5=81=B6=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84II=20,=20=E5=A2=9E=E5=8A=A0golang?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0922.按奇偶排序数组II.md | 28 ++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/problems/0922.按奇偶排序数组II.md b/problems/0922.按奇偶排序数组II.md index 97d7091e..5b66247e 100644 --- a/problems/0922.按奇偶排序数组II.md +++ b/problems/0922.按奇偶排序数组II.md @@ -11,6 +11,8 @@ # 922. 按奇偶排序数组II +[力扣题目链接](https://leetcode-cn.com/problems/sort-array-by-parity-ii/) + 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。 @@ -147,9 +149,9 @@ class Solution { } ``` -## Python +## Python3 -```python3 +```python #方法2 class Solution: def sortArrayByParityII(self, nums: List[int]) -> List[int]: @@ -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 From 34f4a7b7ba3c240db160d710df054c0f8b7732ab Mon Sep 17 00:00:00 2001 From: yqq Date: Sun, 19 Sep 2021 18:13:54 +0800 Subject: [PATCH 4/5] =?UTF-8?q?925.=E9=95=BF=E6=8C=89=E9=94=AE=E5=85=A5=20?= =?UTF-8?q?,=20=E5=A2=9E=E5=8A=A0Golang=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0925.长按键入.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/problems/0925.长按键入.md b/problems/0925.长按键入.md index 3502f2fb..70597508 100644 --- a/problems/0925.长按键入.md +++ b/problems/0925.长按键入.md @@ -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 @@ -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: ----------------------- From 0bb07f39e8e1b2e37ce6866b061afb569025fc14 Mon Sep 17 00:00:00 2001 From: yqq Date: Mon, 20 Sep 2021 16:10:18 +0800 Subject: [PATCH 5/5] =?UTF-8?q?941.=E6=9C=89=E6=95=88=E7=9A=84=E5=B1=B1?= =?UTF-8?q?=E8=84=89=E6=95=B0=E7=BB=84,=20=E5=A2=9E=E5=8A=A0Python,=20Gola?= =?UTF-8?q?ng=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0941.有效的山脉数组.md | 42 ++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/problems/0941.有效的山脉数组.md b/problems/0941.有效的山脉数组.md index 607031da..c4c8ebfa 100644 --- a/problems/0941.有效的山脉数组.md +++ b/problems/0941.有效的山脉数组.md @@ -7,7 +7,7 @@

欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-# 941.有效的山脉数组 +# 941.有效的山脉数组 [力扣题目链接](https://leetcode-cn.com/problems/valid-mountain-array/) @@ -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