From 8887adbb109acd58ac65bea198d1dc287d99db42 Mon Sep 17 00:00:00 2001 From: limuxuale0927 Date: Fri, 14 Jul 2023 21:52:24 +0800 Subject: [PATCH 01/37] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200200.=E5=B2=9B?= =?UTF-8?q?=E5=B1=BF=E6=95=B0=E9=87=8F.=E5=B9=BF=E6=90=9C=E7=89=88.md=20Py?= =?UTF-8?q?thon3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\345\271\277\346\220\234\347\211\210.md" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index 39af9f50a3..4a990a445b 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -196,6 +196,41 @@ class Solution { } } ``` + +Python: + +```python +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + visited = [[False] * n for _ in range(m)] + dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 四个方向 + ans = 0 + + def bfs(x, y): + q = deque() + q.append([x, y]) + visited[x][y] = True + while q: + curx, cury = q.popleft() + for d in dirs: + nextx = curx + d[0] + nexty = cury + d[1] + if nextx < 0 or nextx >= m or nexty < 0 or nexty >= n: # 越界了,直接跳过 + continue + if visited[nextx][nexty] == False and grid[nextx][nexty] == "1": + q.append([nextx, nexty]) + visited[nextx][nexty] = True # 只要加入队列立刻标记 + + for i in range(m): + for j in range(n): + if grid[i][j] == "1" and not visited[i][j]: + ans += 1 # 遇到没访问过的陆地,+1 + bfs(i, j) # 将与其链接的陆地都标记上 true + + return ans +``` +

From 9b5c5ebeea85b133514ab42c0218b79e13599b45 Mon Sep 17 00:00:00 2001 From: limuxuale0927 Date: Fri, 14 Jul 2023 22:07:13 +0800 Subject: [PATCH 02/37] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200200.=E5=B2=9B?= =?UTF-8?q?=E5=B1=BF=E6=95=B0=E9=87=8F.=E6=B7=B1=E6=90=9C=E7=89=88.md=20Py?= =?UTF-8?q?thon3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\346\267\261\346\220\234\347\211\210.md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index c30ace1999..f610e32330 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -218,6 +218,67 @@ class Solution { } } ``` + +Python: + +```python +# 版本一 +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + visited = [[False] * n for _ in range(m)] + dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 四个方向 + result = 0 + + def dfs(x, y): + for d in dirs: + nextx = x + d[0] + nexty = y + d[1] + if nextx < 0 or nextx >= m or nexty < 0 or nexty >= n: # 越界了,直接跳过 + continue + if not visited[nextx][nexty] and grid[nextx][nexty] == '1': # 没有访问过的同时是陆地的 + visited[nextx][nexty] = True + dfs(nextx, nexty) + + for i in range(m): + for j in range(n): + if not visited[i][j] and grid[i][j] == '1': + visited[i][j] = True + result += 1 # 遇到没访问过的陆地,+1 + dfs(i, j) # 将与其链接的陆地都标记上 true + + return result +``` + +```python +# 版本二 +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + visited = [[False] * n for _ in range(m)] + dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 四个方向 + result = 0 + + def dfs(x, y): + if visited[x][y] or grid[x][y] == '0': + return # 终止条件:访问过的节点 或者 遇到海水 + visited[x][y] = True + for d in dirs: + nextx = x + d[0] + nexty = y + d[1] + if nextx < 0 or nextx >= m or nexty < 0 or nexty >= n: # 越界了,直接跳过 + continue + dfs(nextx, nexty) + + for i in range(m): + for j in range(n): + if not visited[i][j] and grid[i][j] == '1': + result += 1 # 遇到没访问过的陆地,+1 + dfs(i, j) # 将与其链接的陆地都标记上 true + + return result +``` +

From 5794b17ab4ccb5da0411896eda509c503014d7d6 Mon Sep 17 00:00:00 2001 From: TonySu Date: Sun, 16 Jul 2023 22:00:05 +0800 Subject: [PATCH 03/37] =?UTF-8?q?Update=200039.=E7=BB=84=E5=90=88=E6=80=BB?= =?UTF-8?q?=E5=92=8C.md,=20=E4=BF=AE=E5=A4=8DPython=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E4=B8=80=E4=B8=AAbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python 版本的回溯剪枝版本一当中, break 会终止整个for 循环,从而提前终止搜索,应该改成continue --- .../0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index 4d9466c3c0..e09a44e496 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -311,7 +311,7 @@ class Solution: for i in range(startIndex, len(candidates)): if total + candidates[i] > target: - break + continue total += candidates[i] path.append(candidates[i]) self.backtracking(candidates, target, total, i, path, result) From a3a8182d28006fbc9d6003dc5907b543c1dba647 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 18 Jul 2023 15:42:47 +0800 Subject: [PATCH 04/37] =?UTF-8?q?Update=200300.=E6=9C=80=E9=95=BF=E4=B8=8A?= =?UTF-8?q?=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97.md=20=E4=BC=98=E5=8C=96=20?= =?UTF-8?q?Rust=20=E5=92=8C=20Java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\207\345\255\220\345\272\217\345\210\227.md" | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index c58c3bf6c4..8287c77719 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -130,18 +130,16 @@ Java: class Solution { public int lengthOfLIS(int[] nums) { int[] dp = new int[nums.length]; + int res = 0; Arrays.fill(dp, 1); - for (int i = 0; i < dp.length; i++) { + for (int i = 1; i < dp.length; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = Math.max(dp[i], dp[j] + 1); } + res = Math.max(res, dp[i]); } } - int res = 0; - for (int i = 0; i < dp.length; i++) { - res = Math.max(res, dp[i]); - } return res; } } @@ -291,7 +289,7 @@ function lengthOfLIS(nums: number[]): number { Rust: ```rust pub fn length_of_lis(nums: Vec) -> i32 { - let mut dp = vec![1; nums.len() + 1]; + let mut dp = vec![1; nums.len()]; let mut result = 1; for i in 1..nums.len() { for j in 0..i { @@ -306,13 +304,6 @@ pub fn length_of_lis(nums: Vec) -> i32 { ``` - - - - - - -

From 826554682f465da29414c4ad6aa9ba391a54e05a Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 18 Jul 2023 16:51:54 +0800 Subject: [PATCH 05/37] =?UTF-8?q?Update=200674.=E6=9C=80=E9=95=BF=E8=BF=9E?= =?UTF-8?q?=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\242\236\345\272\217\345\210\227.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 8cc270ec64..58365df3a7 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -303,6 +303,9 @@ func findLengthOfLCIS(nums []int) int { ``` Rust: + +>动态规划 + ```rust pub fn find_length_of_lcis(nums: Vec) -> i32 { if nums.is_empty() { @@ -320,6 +323,25 @@ pub fn find_length_of_lcis(nums: Vec) -> i32 { } ``` +> 贪心 + +```rust +impl Solution { + pub fn find_length_of_lcis(nums: Vec) -> i32 { + let (mut res, mut count) = (1, 1); + for i in 1..nums.len() { + if nums[i] > nums[i - 1] { + count += 1; + res = res.max(count); + continue; + } + count = 1; + } + res + } +} +``` + Javascript: > 动态规划: From b42feba6057eab0cbb5f25709b3d5d77b3a073c3 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 18 Jul 2023 17:25:21 +0800 Subject: [PATCH 06/37] =?UTF-8?q?Update=200718.=E6=9C=80=E9=95=BF=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=AD=90=E6=95=B0=E7=BB=84.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\255\220\346\225\260\347\273\204.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 82bf4f59e2..5b5e7a6c87 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -537,6 +537,29 @@ function findLength(nums1: number[], nums2: number[]): number { }; ``` +Rust: + +> 滚动数组 + +```rust +impl Solution { + pub fn find_length(nums1: Vec, nums2: Vec) -> i32 { + let (mut res, mut dp) = (0, vec![0; nums2.len()]); + + for n1 in nums1 { + for j in (0..nums2.len()).rev() { + if n1 == nums2[j] { + dp[j] = if j == 0 { 1 } else { dp[j - 1] + 1 }; + res = res.max(dp[j]); + } else { + dp[j] = 0; + } + } + } + res + } +} +``` From 9807ea62e7d2001b9f5c86e9be08b2bce8d1871f Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 18 Jul 2023 19:35:33 +0800 Subject: [PATCH 07/37] =?UTF-8?q?Update=201143.=E6=9C=80=E9=95=BF=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...61\345\255\220\345\272\217\345\210\227.md" | 56 ++++++++++++++----- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 68269b87c5..0bd8b1d606 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -327,23 +327,51 @@ function longestCommonSubsequence(text1: string, text2: string): number { ``` Rust: + +二维 dp: + ```rust -pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { - let (n, m) = (text1.len(), text2.len()); - let (s1, s2) = (text1.as_bytes(), text2.as_bytes()); - let mut dp = vec![0; m + 1]; - let mut last = vec![0; m + 1]; - for i in 1..=n { - dp.swap_with_slice(&mut last); - for j in 1..=m { - dp[j] = if s1[i - 1] == s2[j - 1] { - last[j - 1] + 1 - } else { - last[j].max(dp[j - 1]) - }; +impl Solution { + pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { + let mut dp = vec![vec![0; text2.len() + 1]; text1.len() + 1]; + for (i, c1) in text1.chars().enumerate() { + for (j, c2) in text2.chars().enumerate() { + if c1 == c2 { + dp[i + 1][j + 1] = dp[i][j] + 1; + } else { + dp[i + 1][j + 1] = dp[i][j + 1].max(dp[i + 1][j]); + } + } + } + dp[text1.len()][text2.len()] + } +} +``` + +二维: + +```rust +impl Solution { + pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { + let mut dp = vec![0; text2.len() + 1]; + for c1 in text1.chars() { + // 初始化 prev + let mut prev = 0; + + for (j, c2) in text2.chars().enumerate() { + let temp = dp[j + 1]; + if c1 == c2 { + // 使用上一次的状态,防止重复计算 + dp[j + 1] = prev + 1; + } else { + dp[j + 1] = dp[j + 1].max(dp[j]); + } + // 使用上一次的状态更新 prev + prev = temp; + } } + dp[text2.len()] } - dp[m] } ``` From 695f86743842282e5dae571220af28ee2cac704e Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 18 Jul 2023 22:55:35 +0800 Subject: [PATCH 08/37] =?UTF-8?q?Update=201035.=E4=B8=8D=E7=9B=B8=E4=BA=A4?= =?UTF-8?q?=E7=9A=84=E7=BA=BF.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\344\272\244\347\232\204\347\272\277.md" | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index 7142d75c09..460644ae3e 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -155,21 +155,44 @@ func max(a, b int) int { Rust: ```rust -pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 { - let (n, m) = (nums1.len(), nums2.len()); - let mut last = vec![0; m + 1]; // 记录滚动数组 - let mut dp = vec![0; m + 1]; - for i in 1..=n { - dp.swap_with_slice(&mut last); - for j in 1..=m { - if nums1[i - 1] == nums2[j - 1] { - dp[j] = last[j - 1] + 1; - } else { - dp[j] = last[j].max(dp[j - 1]); +impl Solution { + pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 { + let mut dp = vec![vec![0; nums2.len() + 1]; nums1.len() + 1]; + for (i, num1) in nums1.iter().enumerate() { + for (j, num2) in nums2.iter().enumerate() { + if num1 == num2 { + dp[i + 1][j + 1] = dp[i][j] + 1; + } else { + dp[i + 1][j + 1] = dp[i][j + 1].max(dp[i + 1][j]); + } + } + } + dp[nums1.len()][nums2.len()] + } +} +``` + +> 滚动数组 + +```rust +impl Solution { + pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 { + let mut dp = vec![0; nums2.len() + 1]; + for num1 in nums1 { + let mut prev = 0; + for (j, &num2) in nums2.iter().enumerate() { + let temp = dp[j + 1]; + if num1 == num2 { + // 使用上一次的状态,防止重复计算 + dp[j + 1] = prev + 1; + } else { + dp[j + 1] = dp[j + 1].max(dp[j]); + } + prev = temp; } } + dp[nums2.len()] } - dp[m] } ``` From 1a2c911194a6eb2e9c35f7f179b76553bd48f45c Mon Sep 17 00:00:00 2001 From: Wang Ben Date: Wed, 19 Jul 2023 17:41:44 +0800 Subject: [PATCH 09/37] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201207=20=E7=8B=AC?= =?UTF-8?q?=E4=B8=80=E6=97=A0=E4=BA=8C=E7=9A=84=E5=87=BA=E7=8E=B0=E6=AC=A1?= =?UTF-8?q?=E6=95=B0=20Go=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\272\347\216\260\346\254\241\346\225\260.md" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" index 1a7a001989..d7423b2b7d 100644 --- "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" +++ "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" @@ -135,6 +135,22 @@ class Solution: Go: +```Go +func uniqueOccurrences(arr []int) bool { + count := make(map[int]int) // 统计数字出现的频率 + for _, v := range arr { + count[v] += 1 + } + fre := make(map[int]struct{}) // 看相同频率是否重复出现 + for _, v := range count { + if _, ok := fre[v]; ok { + return false + } + fre[v] = struct{}{} + } + return true +} +``` JavaScript: ``` javascript From eb9c4e647becea279c985e73fd2d4864c002b4a4 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 17:29:22 +0800 Subject: [PATCH 10/37] =?UTF-8?q?Update=200392.=E5=88=A4=E6=96=AD=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\345\255\220\345\272\217\345\210\227.md" | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index c10114c05a..8bb078bd5d 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -258,9 +258,25 @@ func isSubsequence(s string, t string) bool { } ``` - - - +Rust: + +```rust +impl Solution { + pub fn is_subsequence(s: String, t: String) -> bool { + let mut dp = vec![vec![0; t.len() + 1]; s.len() + 1]; + for (i, char_s) in s.chars().enumerate() { + for (j, char_t) in t.chars().enumerate() { + if char_s == char_t { + dp[i + 1][j + 1] = dp[i][j] + 1; + continue; + } + dp[i + 1][j + 1] = dp[i + 1][j] + } + } + dp[s.len()][t.len()] == s.len() + } +} +```

From 503c83db8e9323fe4a94bd824a2b19ff617780d2 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 17:53:05 +0800 Subject: [PATCH 11/37] =?UTF-8?q?Update=200392.=E5=88=A4=E6=96=AD=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\345\255\220\345\272\217\345\210\227.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index 8bb078bd5d..700d682510 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -278,6 +278,30 @@ impl Solution { } ``` +> 滚动数组 + +```rust +impl Solution { + pub fn is_subsequence(s: String, t: String) -> bool { + let mut dp = vec![0; t.len() + 1]; + let (s, t) = (s.as_bytes(), t.as_bytes()); + for &byte_s in s { + let mut pre = 0; + for j in 0..t.len() { + let temp = dp[j + 1]; + if byte_s == t[j] { + dp[j + 1] = pre + 1; + } else { + dp[j + 1] = dp[j]; + } + pre = temp; + } + } + dp[t.len()] == s.len() + } +} +``` +

From f0ac0b2efd588164b74632e75a6c4358e2357781 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 18:57:41 +0800 Subject: [PATCH 12/37] =?UTF-8?q?Update=200115.=E4=B8=8D=E5=90=8C=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\255\220\345\272\217\345\210\227.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index 8c82880d58..56516132ba 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -311,7 +311,31 @@ function numDistinct(s: string, t: string): number { }; ``` +Rust: +```rust +impl Solution { + pub fn num_distinct(s: String, t: String) -> i32 { + if s.len() < t.len() { + return 0; + } + let mut dp = vec![vec![0; s.len() + 1]; t.len() + 1]; + // i = 0, t 为空字符串,s 作为子序列的个数为 1(删除 s 所有元素) + dp[0] = vec![1; s.len() + 1]; + for (i, char_t) in t.chars().enumerate() { + for (j, char_s) in s.chars().enumerate() { + if char_t == char_s { + // t 的前 i 个字符在 s 的前 j 个字符中作为子序列的个数 + dp[i + 1][j + 1] = dp[i][j] + dp[i + 1][j]; + continue; + } + dp[i + 1][j + 1] = dp[i + 1][j]; + } + } + dp[t.len()][s.len()] + } +} +```

From aaa8cc2443a9a05e5af3acae67d7f593c7c34077 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 19:40:19 +0800 Subject: [PATCH 13/37] =?UTF-8?q?Update=200115.=E4=B8=8D=E5=90=8C=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\255\220\345\272\217\345\210\227.md" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index 56516132ba..91f35291cb 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -337,6 +337,36 @@ impl Solution { } ``` +> 滚动数组 + +```rust +impl Solution { + pub fn num_distinct(s: String, t: String) -> i32 { + if s.len() < t.len() { + return 0; + } + let (s, t) = (s.into_bytes(), t.into_bytes()); + // 对于 t 为空字符串,s 作为子序列的个数为 1(删除 s 所有元素) + let mut dp = vec![1; s.len() + 1]; + for char_t in t { + // dp[i - 1][j - 1],dp[j + 1] 更新之前的值 + let mut pre = dp[0]; + // 当开始遍历 t,s 的前 0 个字符无法包含任意子序列 + dp[0] = 0; + for (j, &char_s) in s.iter().enumerate() { + let temp = dp[j + 1]; + if char_t == char_s { + dp[j + 1] = pre + dp[j]; + } else { + dp[j + 1] = dp[j]; + } + pre = temp; + } + } + dp[s.len()] + } +} +```

From 0a83ee2c20c1b1294a2de20f58526755caa785e8 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 19:52:17 +0800 Subject: [PATCH 14/37] =?UTF-8?q?Update=200583.=E4=B8=A4=E4=B8=AA=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4=E6=93=8D=E4=BD=9C?= =?UTF-8?q?.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\351\231\244\346\223\215\344\275\234.md" | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 48d15b0ba9..45e62f7d0f 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -368,7 +368,31 @@ function minDistance(word1: string, word2: string): number { }; ``` - +Rust: + +```rust +impl Solution { + pub fn min_distance(word1: String, word2: String) -> i32 { + let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1]; + for i in 0..word1.len() { + dp[i + 1][0] = i + 1; + } + for j in 0..word2.len() { + dp[0][j + 1] = j + 1; + } + for (i, char1) in word1.chars().enumerate() { + for (j, char2) in word2.chars().enumerate() { + if char1 == char2 { + dp[i + 1][j + 1] = dp[i][j]; + continue; + } + dp[i + 1][j + 1] = dp[i][j + 1].min(dp[i + 1][j]) + 1; + } + } + dp[word1.len()][word2.len()] as i32 + } +} +```

From d6bf0c52cae3da2f9842ba2a30be15099cd53280 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 20:00:17 +0800 Subject: [PATCH 15/37] =?UTF-8?q?Update=200583.=E4=B8=A4=E4=B8=AA=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4=E6=93=8D=E4=BD=9C?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\351\231\244\346\223\215\344\275\234.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 45e62f7d0f..8b5ded3b8d 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -394,6 +394,26 @@ impl Solution { } ``` +> 版本 2 + +```rust +impl Solution { + pub fn min_distance(word1: String, word2: String) -> i32 { + let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1]; + for (i, char1) in word1.chars().enumerate() { + for (j, char2) in word2.chars().enumerate() { + if char1 == char2 { + dp[i + 1][j + 1] = dp[i][j] + 1; + continue; + } + dp[i + 1][j + 1] = dp[i][j + 1].max(dp[i + 1][j]); + } + } + (word1.len() + word2.len() - 2 * dp[word1.len()][word2.len()]) as i32 + } +} +``` +

From a7e35954b094020f7813d90d83a5d7f6ec869a23 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 24 Jul 2023 22:37:56 +0800 Subject: [PATCH 16/37] =?UTF-8?q?Update=200072.=E7=BC=96=E8=BE=91=E8=B7=9D?= =?UTF-8?q?=E7=A6=BB.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...26\350\276\221\350\267\235\347\246\273.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index 703e891311..19e7aade7f 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -401,6 +401,34 @@ int minDistance(char * word1, char * word2){ } ``` +Rust: + +```rust +impl Solution { + pub fn min_distance(word1: String, word2: String) -> i32 { + let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1]; + for i in 1..=word2.len() { + dp[0][i] = i; + } + + for (j, v) in dp.iter_mut().enumerate().skip(1) { + v[0] = j; + } + for (i, char1) in word1.chars().enumerate() { + for (j, char2) in word2.chars().enumerate() { + if char1 == char2 { + dp[i + 1][j + 1] = dp[i][j]; + continue; + } + dp[i + 1][j + 1] = dp[i][j + 1].min(dp[i + 1][j]).min(dp[i][j]) + 1; + } + } + + dp[word1.len()][word2.len()] as i32 + } +} +``` +

From 60bb5dc4fc2288182542422fce73691bc120608f Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 24 Jul 2023 23:27:11 +0800 Subject: [PATCH 17/37] =?UTF-8?q?Update=200072.=E7=BC=96=E8=BE=91=E8=B7=9D?= =?UTF-8?q?=E7=A6=BB.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...26\350\276\221\350\267\235\347\246\273.md" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index 19e7aade7f..15f35f6c8e 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -429,6 +429,36 @@ impl Solution { } ``` +> 一维 dp + +```rust +impl Solution { + pub fn min_distance(word1: String, word2: String) -> i32 { + let mut dp = vec![0; word1.len() + 1]; + for (i, v) in dp.iter_mut().enumerate().skip(1) { + *v = i; + } + + for char2 in word2.chars() { + // 相当于 dp[i][0] 的初始化 + let mut pre = dp[0]; + dp[0] += 1; // j = 0, 将前 i 个字符变成空串的个数 + for (j, char1) in word1.chars().enumerate() { + let temp = dp[j + 1]; + if char1 == char2 { + dp[j + 1] = pre; + } else { + dp[j + 1] = dp[j + 1].min(dp[j]).min(pre) + 1; + } + pre = temp; + } + } + + dp[word1.len()] as i32 + } +} +``` +

From 69ad28ca0ecbcd726619d5aad81351d4e275db54 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 24 Jul 2023 23:57:57 +0800 Subject: [PATCH 18/37] =?UTF-8?q?Update=200647.=E5=9B=9E=E6=96=87=E5=AD=90?= =?UTF-8?q?=E4=B8=B2.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\346\226\207\345\255\220\344\270\262.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index 084b9f74e6..35396192c0 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -517,8 +517,33 @@ function expandRange(s: string, left: number, right: number): number { } ``` +Rust: +> 双指针 + +```rust +impl Solution { + pub fn count_substrings(s: String) -> i32 { + let mut res = 0; + for i in 0..s.len() { + res += Self::extend(&s, i, i, s.len()); + res += Self::extend(&s, i, i + 1, s.len()); + } + res + } + + fn extend(s: &str, mut i: usize, mut j: usize, len: usize) -> i32 { + let mut res = 0; + while i < len && j < len && s[i..=i] == s[j..=j] { + res += 1; + i = i.wrapping_sub(1); + j += 1; + } + res + } +} +```

From 6011184b37f9e1d661edef1b124b5e195ef55bd9 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 25 Jul 2023 00:18:19 +0800 Subject: [PATCH 19/37] =?UTF-8?q?Update=200647.=E5=9B=9E=E6=96=87=E5=AD=90?= =?UTF-8?q?=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...236\346\226\207\345\255\220\344\270\262.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index 35396192c0..52f12d9b03 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -519,6 +519,24 @@ function expandRange(s: string, left: number, right: number): number { Rust: +```rust +impl Solution { + pub fn count_substrings(s: String) -> i32 { + let mut dp = vec![vec![false; s.len()]; s.len()]; + let mut res = 0; + + for i in (0..s.len()).rev() { + for j in i..s.len() { + if s[i..=i] == s[j..=j] && (j - i <= 1 || dp[i + 1][j - 1]) { + dp[i][j] = true; + res += 1; + } + } + } + res + } +} +``` > 双指针 From 4822d8ff8cb71d4787a4d6a3f33ecba98fbc9be3 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 25 Jul 2023 00:45:10 +0800 Subject: [PATCH 20/37] =?UTF-8?q?Update=200516.=E6=9C=80=E9=95=BF=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E5=AD=90=E5=BA=8F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...07\345\255\220\345\272\217\345\210\227.md" | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" index fcdd57b0f4..2180bab8d9 100644 --- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" @@ -274,7 +274,26 @@ function longestPalindromeSubseq(s: string): number { }; ``` - +Rust: + +```rust +impl Solution { + pub fn longest_palindrome_subseq(s: String) -> i32 { + let mut dp = vec![vec![0; s.len()]; s.len()]; + for i in (0..s.len()).rev() { + dp[i][i] = 1; + for j in i + 1..s.len() { + if s[i..=i] == s[j..=j] { + dp[i][j] = dp[i + 1][j - 1] + 2; + continue; + } + dp[i][j] = dp[i + 1][j].max(dp[i][j - 1]); + } + } + dp[0][s.len() - 1] + } +} +```

From 3933b49cb36eaacb1fca0d40861386722b32347e Mon Sep 17 00:00:00 2001 From: han Date: Wed, 26 Jul 2023 17:21:15 +0800 Subject: [PATCH 21/37] =?UTF-8?q?=E6=B7=BB=E5=8A=A01002.=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E5=B8=B8=E7=94=A8=E5=AD=97=E7=AC=A6=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\347\224\250\345\255\227\347\254\246.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" index a53148b313..81b07b3943 100644 --- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" +++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" @@ -518,6 +518,54 @@ impl Solution { } ``` +Ruby: +```ruby +def common_chars(words) + result = [] + #统计所有字符串里字符出现的最小频率 + hash = {} + #初始化标识 + is_first = true + + words.each do |word| + #记录共同字符 + chars = [] + word.split('').each do |chr| + #第一个字符串初始化 + if is_first + chars << chr + else + #字母之前出现过的最小次数 + if hash[chr] != nil && hash[chr] > 0 + hash[chr] -= 1 + chars << chr + end + end + end + + is_first = false + #清除hash,更新字符最小频率 + hash.clear + chars.each do |chr| + if hash[chr] != nil + hash[chr] += 1 + else + hash[chr] = 1 + end + end + end + + #字符最小频率hash转换为字符数组 + hash.keys.each do |key| + for i in 0..hash[key] - 1 + result << key + end + end + + return result +end +``` +

From 3f0966382e37245b96e7967f95e725f37c2cbdc6 Mon Sep 17 00:00:00 2001 From: leslieCHUENGT <1278207976@qq.com> Date: Thu, 27 Jul 2023 09:56:18 +0800 Subject: [PATCH 22/37] =?UTF-8?q?=E6=B7=BB=E5=8A=A00463=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E7=9A=84=E5=91=A8=E9=95=BF=20TypeScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\347\232\204\345\221\250\351\225\277.md" | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 18f1d01eb2..35b89dcf87 100644 --- "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -300,6 +300,104 @@ var islandPerimeter = function(grid) { }; ``` +TypeScript: + +```typescript +/** + * 方法一:深度优先搜索(DFS) + * @param grid 二维网格地图,其中 grid[i][j] = 1 表示陆地, grid[i][j] = 0 表示水域 + * @returns 岛屿的周长 + */ +function islandPerimeter(grid: number[][]): number { + // 处理特殊情况:网格为空或行列数为 0,直接返回 0 + if (!grid || grid.length === 0 || grid[0].length === 0) { + return 0; + } + + // 获取网格的行数和列数 + const rows = grid.length; + const cols = grid[0].length; + let perimeter = 0; // 岛屿的周长 + + /** + * 深度优先搜索函数 + * @param i 当前格子的行索引 + * @param j 当前格子的列索引 + */ + const dfs = (i: number, j: number) => { + // 如果当前位置超出网格范围,或者当前位置是水域(grid[i][j] === 0),则周长增加1 + if (i < 0 || i >= rows || j < 0 || j >= cols || grid[i][j] === 0) { + perimeter++; + return; + } + + // 如果当前位置已经访问过(grid[i][j] === -1),则直接返回 + if (grid[i][j] === -1) { + return; + } + + // 标记当前位置为已访问(-1),避免重复计算 + grid[i][j] = -1; + + // 继续搜索上、下、左、右四个方向 + dfs(i + 1, j); + dfs(i - 1, j); + dfs(i, j + 1); + dfs(i, j - 1); + }; + + // 遍历整个网格,找到第一个陆地格子(grid[i][j] === 1),并以此为起点进行深度优先搜索 + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (grid[i][j] === 1) { + dfs(i, j); + break; + } + } + } + + return perimeter; +} + +/** + * 方法二:遍历每个陆地格子,统计周长 + * @param grid 二维网格地图,其中 grid[i][j] = 1 表示陆地, grid[i][j] = 0 表示水域 + * @returns 岛屿的周长 + */ +function islandPerimeter(grid: number[][]): number { + // 处理特殊情况:网格为空或行列数为 0,直接返回 0 + if (!grid || grid.length === 0 || grid[0].length === 0) { + return 0; + } + + // 获取网格的行数和列数 + const rows = grid.length; + const cols = grid[0].length; + let perimeter = 0; // 岛屿的周长 + + // 遍历整个网格 + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + // 如果当前格子是陆地(grid[i][j] === 1) + if (grid[i][j] === 1) { + perimeter += 4; // 周长先加上4个边 + + // 判断当前格子的上方是否也是陆地,如果是,则周长减去2个边 + if (i > 0 && grid[i - 1][j] === 1) { + perimeter -= 2; + } + + // 判断当前格子的左方是否也是陆地,如果是,则周长减去2个边 + if (j > 0 && grid[i][j - 1] === 1) { + perimeter -= 2; + } + } + } + } + + return perimeter; +} +```

From 7ee623f4da4e6615f867d2456a6ec174362f0100 Mon Sep 17 00:00:00 2001 From: han Date: Thu, 27 Jul 2023 16:27:19 +0800 Subject: [PATCH 23/37] =?UTF-8?q?=E6=B7=BB=E5=8A=A00349.=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86=20Ruby=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\347\232\204\344\272\244\351\233\206.md" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 8daf5a35ba..26a9286dc9 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -465,6 +465,25 @@ object Solution { } ``` + +###Ruby +```ruby +def intersection(nums1, nums2) + hash = {} + result = {} + + nums1.each do |num| + hash[num] = 1 if hash[num].nil? + end + + nums2.each do |num| + #取nums1和nums2交集 + result[num] = 1 if hash[num] != nil + end + + return result.keys +end +``` ## 相关题目 * [350.两个数组的交集 II](https://leetcode.cn/problems/intersection-of-two-arrays-ii/) From 5dc3022bc5c6aa84fdd1ce255fad723200ffe059 Mon Sep 17 00:00:00 2001 From: slaier <30682486+slaier@users.noreply.github.com> Date: Sat, 29 Jul 2023 06:48:13 +0000 Subject: [PATCH 24/37] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200202=E5=BF=AB?= =?UTF-8?q?=E4=B9=90=E6=95=B0=20C=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\345\277\253\344\271\220\346\225\260.md" | 68 ++++++++++++++----- 1 file changed, 50 insertions(+), 18 deletions(-) diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" index 4a77e2b67c..719672a281 100644 --- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" +++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" @@ -423,29 +423,61 @@ impl Solution { ### C: ```C -typedef struct HashNodeTag { - int key; /* num */ - struct HashNodeTag *next; -}HashNode; - -/* Calcualte the hash key */ -static inline int hash(int key, int size) { - int index = key % size; - return (index > 0) ? (index) : (-index); -} - -/* Calculate the sum of the squares of its digits*/ -static inline int calcSquareSum(int num) { - unsigned int sum = 0; - while(num > 0) { - sum += (num % 10) * (num % 10); - num = num/10; +int get_sum(int n) { + int sum = 0; + div_t n_div = { .quot = n }; + while (n_div.quot != 0) { + n_div = div(n_div.quot, 10); + sum += n_div.rem * n_div.rem; } return sum; } +// (版本1)使用数组 +bool isHappy(int n) { + // sum = a1^2 + a2^2 + ... ak^2 + // first round: + // 1 <= k <= 10 + // 1 <= sum <= 1 + 81 * 9 = 730 + // second round: + // 1 <= k <= 3 + // 1 <= sum <= 36 + 81 * 2 = 198 + // third round: + // 1 <= sum <= 81 * 2 = 162 + // fourth round: + // 1 <= sum <= 81 * 2 = 162 + + uint8_t visited[163] = { 0 }; + int sum = get_sum(get_sum(n)); + int next_n = sum; + + while (next_n != 1) { + sum = get_sum(next_n); + + if (visited[sum]) return false; + + visited[sum] = 1; + next_n = sum; + }; + + return true; +} + +// (版本2)使用快慢指针 +bool isHappy(int n) { + int slow = n; + int fast = n; + + do { + slow = get_sum(slow); + fast = get_sum(get_sum(fast)); + } while (slow != fast); + + return (fast == 1); +} +``` -Scala: +### Scala: ```scala object Solution { // 引入mutable From dca8209aaf3276ba0d639165d13a27b21a24c498 Mon Sep 17 00:00:00 2001 From: Binbin Date: Mon, 31 Jul 2023 14:17:07 +0800 Subject: [PATCH 25/37] Fix typos and cleanups --- .../0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" | 2 +- "problems/0383.\350\265\216\351\207\221\344\277\241.md" | 2 +- ...72\257\345\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- ...23\210\345\270\214\350\241\250\346\200\273\347\273\223.md" | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index cdf33c5812..37d5614e71 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -73,7 +73,7 @@ candidates 中的数字可以无限制重复被选取。 如果是多个集合取组合,各个集合之间相互不影响,那么就不用startIndex,例如:[17.电话号码的字母组合](https://programmercarl.com/0017.电话号码的字母组合.html) -**注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路,后面我再讲解排列的时候就重点介绍**。 +**注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路,后面我在讲解排列的时候会重点介绍**。 代码如下: diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index c15d1ac177..3de48ce3da 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -68,7 +68,7 @@ public: ### 哈希解法 -因为题目所只有小写字母,那可以采用空间换取时间的哈希策略, 用一个长度为26的数组还记录magazine里字母出现的次数。 +因为题目说只有小写字母,那可以采用空间换取时间的哈希策略,用一个长度为26的数组来记录magazine里字母出现的次数。 然后再用ransomNote去验证这个数组是否包含了ransomNote所需要的所有字母。 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index 76bd331bd4..3f1d10126e 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -17,7 +17,7 @@ 如果是多个集合取组合,各个集合之间相互不影响,那么就不用startIndex,例如:[回溯算法:电话号码的字母组合](https://programmercarl.com/0017.电话号码的字母组合.html) -**注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路,后面我再讲解排列的时候就重点介绍**。 +**注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路,后面我在讲解排列的时候会重点介绍**。 最后还给出了本题的剪枝优化,如下: diff --git "a/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" "b/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" index 6750636305..465ef9d1da 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" @@ -16,7 +16,7 @@ **一般来说哈希表都是用来快速判断一个元素是否出现集合里**。 -对于哈希表,要知道**哈希函数**和**哈希碰撞**在哈希表中的作用. +对于哈希表,要知道**哈希函数**和**哈希碰撞**在哈希表中的作用。 哈希函数是把传入的key映射到符号表的索引上。 @@ -88,7 +88,7 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底 map是一种``的结构,本题可以用key保存数值,用value在保存数值所在的下标。所以使用map最为合适。 -C++提供如下三种map::(详情请看[关于哈希表,你该了解这些!](https://programmercarl.com/哈希表理论基础.html)) +C++提供如下三种map:(详情请看[关于哈希表,你该了解这些!](https://programmercarl.com/哈希表理论基础.html)) * std::map * std::multimap From 5bd60c95b709ee5f56399c70ea6b93bd46d38a6b Mon Sep 17 00:00:00 2001 From: Martin <87409284+martinchiu@users.noreply.github.com> Date: Mon, 31 Jul 2023 22:44:59 +0800 Subject: [PATCH 26/37] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=2054.=E8=9E=BA?= =?UTF-8?q?=E6=97=8B=E7=9F=A9=E9=98=B5=20Javascript=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\346\227\213\347\237\251\351\230\265.md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index d855f1a166..c62eb2b12c 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -200,6 +200,58 @@ class Solution { } ``` +### Javascript +``` +/** + * @param {number[][]} matrix + * @return {number[]} + */ +var spiralOrder = function(matrix) { + let m = matrix.length + let n = matrix[0].length + + let startX = startY = 0 + let i = 0 + let arr = new Array(m*n).fill(0) + let offset = 1 + let loop = mid = Math.floor(Math.min(m,n) / 2) + while (loop--) { + let row = startX + let col = startY + // --> + for (; col < n + startY - offset; col++) { + arr[i++] = matrix[row][col] + } + // down + for (; row < m + startX - offset; row++) { + arr[i++] = matrix[row][col] + } + // <-- + for (; col > startY; col--) { + arr[i++] = matrix[row][col] + } + for (; row > startX; row--) { + arr[i++] = matrix[row][col] + } + startX++ + startY++ + offset += 2 + } + if (Math.min(m, n) % 2 === 1) { + if (n > m) { + for (let j = mid; j < mid + n - m + 1; j++) { + arr[i++] = matrix[mid][j] + } + } else { + for (let j = mid; j < mid + m - n + 1; j++) { + arr[i++] = matrix[j][mid] + } + } + } + return arr +}; +``` +

From 3f428289b2d6cc2cd75b163d1127ff374f7de116 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 5 Aug 2023 15:55:18 +0800 Subject: [PATCH 27/37] =?UTF-8?q?Update=200084.=E6=9F=B1=E7=8A=B6=E5=9B=BE?= =?UTF-8?q?=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2.md=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=87=8D=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\347\232\204\347\237\251\345\275\242.md" | 56 ------------------- 1 file changed, 56 deletions(-) diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index 9b76229af0..9fb6a6b061 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -725,62 +725,6 @@ impl Solution { } ``` -Rust - -双指针预处理 -```rust - -impl Solution { - pub fn largest_rectangle_area(v: Vec) -> i32 { - let n = v.len(); - let mut left_smaller_idx = vec![-1; n]; - let mut right_smaller_idx = vec![n as i32; n]; - for i in 1..n { - let mut mid = i as i32 - 1; - while mid >= 0 && v[mid as usize] >= v[i] { - mid = left_smaller_idx[mid as usize]; - } - left_smaller_idx[i] = mid; - } - for i in (0..n-1).rev() { - let mut mid = i + 1; - while mid < n && v[mid] >= v[i] { - mid = right_smaller_idx[mid] as usize; - } - right_smaller_idx[i] = mid as i32; - } - let mut res = 0; - for (idx, &e) in v.iter().enumerate() { - res = res.max((right_smaller_idx[idx] - left_smaller_idx[idx] - 1) * e); - } - dbg!(res) - } -} -``` - -单调栈 -```rust -impl Solution { - pub fn largest_rectangle_area1(mut v: Vec) -> i32 { - v.insert(0, 0); // 便于使第一个元素能够有左侧<=它的值 - v.push(0); // 便于在结束处理最后一个元素后清空残留在栈中的值 - let mut res = 0; - let mut stack = vec![]; // 递增的栈 - for (idx, &e) in v.iter().enumerate() { - while !stack.is_empty() && v[*stack.last().unwrap()] > e { - let pos = stack.pop().unwrap(); - let prev_pos = *stack.last().unwrap(); - let s = (idx - prev_pos - 1) as i32 * v[pos]; - res = res.max(s); - } - stack.push(idx); - } - res - } -} -``` - -

From dc65f95f7beecb1f6b2ecf49665b4f3a89c6c904 Mon Sep 17 00:00:00 2001 From: wisuky <48423733+wisuky@users.noreply.github.com> Date: Sat, 5 Aug 2023 17:05:35 +0800 Subject: [PATCH 28/37] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改错别字 --- ...001.\344\270\244\346\225\260\344\271\213\345\222\214.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index 712bc3f0df..4e44d0c3f6 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -37,20 +37,20 @@ [242. 有效的字母异位词](https://www.programmercarl.com/0242.有效的字母异位词.html) 这道题目是用数组作为哈希表来解决哈希问题,[349. 两个数组的交集](https://www.programmercarl.com/0349.两个数组的交集.html)这道题目是通过set作为哈希表来解决哈希问题。 -首先我在强调一下 **什么时候使用哈希法**,当我们需要查询一个元素是否出现过,或者一个元素是否在集合里的时候,就要第一时间想到哈希法。 +首先我再强调一下 **什么时候使用哈希法**,当我们需要查询一个元素是否出现过,或者一个元素是否在集合里的时候,就要第一时间想到哈希法。 本题呢,我就需要一个集合来存放我们遍历过的元素,然后在遍历数组的时候去询问这个集合,某元素是否遍历过,也就是 是否出现在这个集合。 那么我们就应该想到使用哈希法了。 -因为本地,我们不仅要知道元素有没有遍历过,还要知道这个元素对应的下标,**需要使用 key value结构来存放,key来存元素,value来存下标,那么使用map正合适**。 +因为本题,我们不仅要知道元素有没有遍历过,还要知道这个元素对应的下标,**需要使用 key value结构来存放,key来存元素,value来存下标,那么使用map正合适**。 再来看一下使用数组和set来做哈希法的局限。 * 数组的大小是受限制的,而且如果元素很少,而哈希值太大会造成内存空间的浪费。 * set是一个集合,里面放的元素只能是一个key,而两数之和这道题目,不仅要判断y是否存在而且还要记录y的下标位置,因为要返回x 和 y的下标。所以set 也不能用。 -此时就要选择另一种数据结构:map ,map是一种key value的存储结构,可以用key保存数值,用value在保存数值所在的下标。 +此时就要选择另一种数据结构:map ,map是一种key value的存储结构,可以用key保存数值,用value再保存数值所在的下标。 C++中map,有三种类型: From d4810327aed53170c21c23f06e1fb6c54a184337 Mon Sep 17 00:00:00 2001 From: Liu Yongliang <41845017+tlylt@users.noreply.github.com> Date: Sun, 6 Aug 2023 10:25:37 +0800 Subject: [PATCH 29/37] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index f99102ab45..177c3878ba 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -52,7 +52,7 @@ 也就是由前后相同的子串组成。 -那么既然前面有相同的子串,后面有相同的子串,用 s + s,这样组成的字符串中,后面的子串做前串,前后的子串做后串,就一定还能组成一个s,如图: +那么既然前面有相同的子串,后面有相同的子串,用 s + s,这样组成的字符串中,后面的子串做前串,前面的子串做后串,就一定还能组成一个s,如图: ![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220728104931.png) From 692647b03a1e14d895bbb24b5b98286c17100c73 Mon Sep 17 00:00:00 2001 From: wisuky <48423733+wisuky@users.noreply.github.com> Date: Sun, 6 Aug 2023 21:27:09 +0800 Subject: [PATCH 30/37] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0=E7=9B=B8?= =?UTF-8?q?=E5=8A=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index 90c3733449..1c262c873d 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -41,7 +41,7 @@ ## 思路 -本题咋眼一看好像和[0015.三数之和](https://programmercarl.com/0015.三数之和.html),[0018.四数之和](https://programmercarl.com/0018.四数之和.html)差不多,其实差很多。 +本题乍眼一看好像和[0015.三数之和](https://programmercarl.com/0015.三数之和.html),[0018.四数之和](https://programmercarl.com/0018.四数之和.html)差不多,其实差很多。 **本题是使用哈希法的经典题目,而[0015.三数之和](https://programmercarl.com/0015.三数之和.html),[0018.四数之和](https://programmercarl.com/0018.四数之和.html)并不合适使用哈希法**,因为三数之和和四数之和这两道题目使用哈希法在不超时的情况下做到对结果去重是很困难的,很有多细节需要处理。 From 1f9f9c224cafb201ae71668a330a82a01105cfd5 Mon Sep 17 00:00:00 2001 From: wisuky <48423733+wisuky@users.noreply.github.com> Date: Tue, 8 Aug 2023 13:43:15 +0800 Subject: [PATCH 31/37] =?UTF-8?q?Update=200015.=E4=B8=89=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...015.\344\270\211\346\225\260\344\271\213\345\222\214.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" index 4951c90cf1..91fc9d685a 100644 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -171,7 +171,7 @@ public: #### a的去重 -说道去重,其实主要考虑三个数的去重。 a, b ,c, 对应的就是 nums[i],nums[left],nums[right] +说到去重,其实主要考虑三个数的去重。 a, b ,c, 对应的就是 nums[i],nums[left],nums[right] a 如果重复了怎么办,a是nums里遍历的元素,那么应该直接跳过去。 @@ -181,7 +181,7 @@ a 如果重复了怎么办,a是nums里遍历的元素,那么应该直接跳 其实不一样! -都是和 nums[i]进行比较,是比较它的前一个,还是比较他的后一个。 +都是和 nums[i]进行比较,是比较它的前一个,还是比较它的后一个。 如果我们的写法是 这样: @@ -191,7 +191,7 @@ if (nums[i] == nums[i + 1]) { // 去重操作 } ``` -那就我们就把 三元组中出现重复元素的情况直接pass掉了。 例如{-1, -1 ,2} 这组数据,当遍历到第一个-1 的时候,判断 下一个也是-1,那这组数据就pass了。 +那我们就把 三元组中出现重复元素的情况直接pass掉了。 例如{-1, -1 ,2} 这组数据,当遍历到第一个-1 的时候,判断 下一个也是-1,那这组数据就pass了。 **我们要做的是 不能有重复的三元组,但三元组内的元素是可以重复的!** From d3eb234491ba63256ab8451ca16fd1084d24b8ff Mon Sep 17 00:00:00 2001 From: Jining Jiang <58833386+jjnnzb@users.noreply.github.com> Date: Tue, 8 Aug 2023 23:51:25 +0800 Subject: [PATCH 32/37] =?UTF-8?q?fix:=20=E8=A1=A8=E8=BF=B0=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 应该是“其他”,而不是“其其他” --- ...\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index 7511ac8715..79ad26a859 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -28,7 +28,7 @@ ![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117171307407.png) -至于背包九讲其其他背包,面试几乎不会问,都是竞赛级别的了,leetcode上连多重背包的题目都没有,所以题库也告诉我们,01背包和完全背包就够用了。 +至于背包九讲其他背包,面试几乎不会问,都是竞赛级别的了,leetcode上连多重背包的题目都没有,所以题库也告诉我们,01背包和完全背包就够用了。 而完全背包又是也是01背包稍作变化而来,即:完全背包的物品数量是无限的。 From 6167408cd29d884bb8b74cc73b05ce7b6aac87ed Mon Sep 17 00:00:00 2001 From: han Date: Wed, 9 Aug 2023 12:06:32 +0800 Subject: [PATCH 33/37] =?UTF-8?q?=E6=B7=BB=E5=8A=A00018.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...33\346\225\260\344\271\213\345\222\214.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 28c20b7acd..17715b2e6f 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -649,6 +649,54 @@ object Solution { } } ``` +### Ruby: + +```ruby +def four_sum(nums, target) + #结果集 + result = [] + nums = nums.sort! + + for i in 0..nums.size - 1 + return result if i > 0 && nums[i] > target && nums[i] >= 0 + #对a进行去重 + next if i > 0 && nums[i] == nums[i - 1] + + for j in i + 1..nums.size - 1 + break if nums[i] + nums[j] > target && nums[i] + nums[j] >= 0 + #对b进行去重 + next if j > i + 1 && nums[j] == nums[j - 1] + left = j + 1 + right = nums.size - 1 + while left < right + sum = nums[i] + nums[j] + nums[left] + nums[right] + if sum > target + right -= 1 + elsif sum < target + left += 1 + else + result << [nums[i], nums[j], nums[left], nums[right]] + + #对c进行去重 + while left < right && nums[left] == nums[left + 1] + left += 1 + end + + #对d进行去重 + while left < right && nums[right] == nums[right - 1] + right -= 1 + end + + right -= 1 + left += 1 + end + end + end + end + + return result +end +```

From 7ae878753b3d700d14a3bfd0c55194c618b62a03 Mon Sep 17 00:00:00 2001 From: han Date: Thu, 10 Aug 2023 10:21:29 +0800 Subject: [PATCH 34/37] =?UTF-8?q?=E6=B7=BB=E5=8A=A01047.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80=E6=9C=89?= =?UTF-8?q?=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9=20Ruby=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\351\207\215\345\244\215\351\241\271.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" index ad54f0f88e..ffe1353039 100644 --- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" +++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" @@ -475,6 +475,26 @@ impl Solution { } ``` +### Ruby + +```ruby +def remove_duplicates(s) + #数组模拟栈 + stack = [] + s.each_char do |chr| + if stack.empty? + stack.push chr + else + head = stack.pop + #重新进栈 + stack.push head, chr if head != chr + end + end + + return stack.join +end +``` +

From 86f2dcb46bbd7e243bee746c37f7cdeb448f004c Mon Sep 17 00:00:00 2001 From: TonySu Date: Thu, 10 Aug 2023 17:24:51 +0800 Subject: [PATCH 35/37] =?UTF-8?q?Update=200130.=E8=A2=AB=E5=9B=B4=E7=BB=95?= =?UTF-8?q?=E7=9A=84=E5=8C=BA=E5=9F=9F.md,=20=E5=A2=9E=E5=8A=A0Python3=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E7=9A=84DFS=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加Python3 版本的DFS解法,在leetcode上测试通过。 --- ...25\347\232\204\345\214\272\345\237\237.md" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" index e244873ba3..e8a1f02f69 100644 --- "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" +++ "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" @@ -385,6 +385,55 @@ class Solution { } } ``` +### Python3 + +```Python +// 深度优先遍历 +class Solution: + dir_list = [(0, 1), (0, -1), (1, 0), (-1, 0)] + def solve(self, board: List[List[str]]) -> None: + """ + Do not return anything, modify board in-place instead. + """ + row_size = len(board) + column_size = len(board[0]) + visited = [[False] * column_size for _ in range(row_size)] + # 从边缘开始,将边缘相连的O改成A。然后遍历所有,将A改成O,O改成X + # 第一行和最后一行 + for i in range(column_size): + if board[0][i] == "O" and not visited[0][i]: + self.dfs(board, 0, i, visited) + if board[row_size-1][i] == "O" and not visited[row_size-1][i]: + self.dfs(board, row_size-1, i, visited) + + # 第一列和最后一列 + for i in range(1, row_size-1): + if board[i][0] == "O" and not visited[i][0]: + self.dfs(board, i, 0, visited) + if board[i][column_size-1] == "O" and not visited[i][column_size-1]: + self.dfs(board, i, column_size-1, visited) + + for i in range(row_size): + for j in range(column_size): + if board[i][j] == "A": + board[i][j] = "O" + elif board[i][j] == "O": + board[i][j] = "X" + + + def dfs(self, board, x, y, visited): + if visited[x][y] or board[x][y] == "X": + return + visited[x][y] = True + board[x][y] = "A" + for i in range(4): + new_x = x + self.dir_list[i][0] + new_y = y + self.dir_list[i][1] + if new_x >= len(board) or new_y >= len(board[0]) or new_x < 0 or new_y < 0: + continue + self.dfs(board, new_x, new_y, visited) + +```

From 6a2f01055c784d54059568f970b44d650be04e87 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Tue, 15 Aug 2023 10:09:47 +0800 Subject: [PATCH 36/37] Update --- ...11\346\216\222\350\241\214\347\250\213.md" | 7 +- problems/qita/acm.md | 89 +++++++++++++++++++ ...06\350\256\272\345\237\272\347\241\200.md" | 2 +- 3 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 problems/qita/acm.md diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 3798b48d87..794a67fa40 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -237,13 +237,10 @@ public: ```cpp for (pair& target : targets[result[result.size() - 1]]) ``` -pair里要有const,因为map中的key是不可修改的,所以是`pair`。 -如果不加const,也可以复制一份pair,例如这么写: +一定要加上引用即 `& target`,因为后面有对 target.second 做减减操作,如果没有引用,单纯复制,这个结果就没记录下来,那最后的结果就不对了。 -```cpp -for (pairtarget : targets[result[result.size() - 1]]) -``` +加上引用之后,就必须在 string 前面加上 const,因为map中的key 是不可修改了,这就是语法规定了。 ## 总结 diff --git a/problems/qita/acm.md b/problems/qita/acm.md new file mode 100644 index 0000000000..1be0e924de --- /dev/null +++ b/problems/qita/acm.md @@ -0,0 +1,89 @@ + +# 如何练习ACM模式输入输入模式 | 如何准备笔试 | 卡码网 + +卡码网地址:[https://kamacoder.com](https://kamacoder.com) + +## 为什么卡码网 + +录友们在求职的时候会发现,很多公司的笔试题和面试题都是ACM模式, 而大家习惯去力扣刷题,力扣是核心代码模式。 + +当大家在做ACM模式的算法题的时候,需要自己处理数据的输入输出,**如果没有接触过的话,还是挺难的**。 + +[知识星球](https://programmercarl.com/other/kstar.html)里很多录友的日常打卡中,都表示被 ACM模式折磨过: + +

+ +
+ +
+ +
+ +
+ +所以我正式推出:**卡码网**([https://kamacoder.com](https://kamacoder.com)),**专门帮助大家练习ACM模式**。 + +那么之前大家去哪里练习ACM模式呢? + +去牛客做笔试真题,结果发现 ACM模式没练出来,题目倒是巨难,一点思路都没有,代码更没有写,ACM模式无从练起。 + +去洛谷,POJ上练习? 结果发现 题目超多,不知道从哪里开始刷,也没有一个循序渐进的刷题顺序。 + +**而卡码网上有我精选+制作的25道题目**!我还把25题的后台测试数据制作了一遍,保证大家练习的效果。 + +为什么题目不多,只有25道? + +因为大家练习ACM模式不需要那么多题目,有一个循序渐进的练习过程就好了。 + +这25道题目包含了数组、链表、哈希表、字符串、二叉树、动态规划以及图的的题目,常见的输入输出方式都覆盖了。 + +**这是最精华的25道题目**!。 + +## 卡码网长什么样 + +来看看这极简的界面,没有烂七八糟的功能,只有刷题! + +
+ +在「状态」这里可以看到 大家提交的代码和判题记录,目前卡码网([https://kamacoder.com](https://kamacoder.com))几乎无时无刻都有卡友在提交代码。 +看看大家周六晚上都在做什么,刷哪些题目。 + +
+ + +提交代码的界面是这样的,**目前支持所有主流刷题语言**。 + +
+ +## 题解 + +基本大家来卡码网([https://kamacoder.com](https://kamacoder.com))练习ACM模式,都是对输入输出不够了解的,所以想看现成的题解,看看究竟是怎么处理的。 + +所以我用C++把卡码网上25道题目的题解都写了,并发布到Github上: + +[https://github.com/youngyangyang04/kamacoder-solutions](https://github.com/youngyangyang04/kamacoder-solutions) + +
+ +**欢迎去Github上star,欢迎fork,也欢迎来Github仓库贡献其他语言版本,成为contributor**。 + +如果不懂如何和开源项目提交代码,[可以看这里](https://www.programmercarl.com/qita/join.html) + +目前已经有两位录友贡献C和Java版本了。 + +
+ +期待在Github(https://github.com/youngyangyang04/kamacoder-solutions) 的contributors上也出现你的头像。 + +目前题解只有C++代码吗? + +当然不是,大多数题目已经有了 Java、python、C版本。 **其他语言版本,就给录友们成为contributor的机会了**。 + +## 最后 + +卡码网地址:[https://kamacoder.com](https://kamacoder.com) + +快去体验吧,笔试之前最好 把卡码网25道题目都刷完。 + +期待录友们成为最早一批把卡码网刷爆的coder! + diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index 184dba604c..e2c6d83c59 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -150,7 +150,7 @@ 最后再说一说二叉树中深度优先和广度优先遍历实现方式,我们做二叉树相关题目,经常会使用递归的方式来实现深度优先遍历,也就是实现前中后序遍历,使用递归是比较方便的。 -**之前我们讲栈与队列的时候,就说过栈其实就是递归的一种实现结构**,也就说前中后序遍历的逻辑其实都是可以借助栈使用非递归的方式来实现的。 +**之前我们讲栈与队列的时候,就说过栈其实就是递归的一种实现结构**,也就说前中后序遍历的逻辑其实都是可以借助栈使用递归的方式来实现的。 而广度优先遍历的实现一般使用队列来实现,这也是队列先进先出的特点所决定的,因为需要先进先出的结构,才能一层一层的来遍历二叉树。 From b66eb1c7fddaafdf28e510bbfe59fdb5107c67b3 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 18 Aug 2023 09:35:21 +0800 Subject: [PATCH 37/37] Update --- ...\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" | 2 +- ...\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" | 2 +- ...\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index ce9a247c1f..51d030e784 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -18,7 +18,7 @@ * [102.二叉树的层序遍历](https://leetcode.cn/problems/binary-tree-level-order-traversal/) * [107.二叉树的层次遍历II](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/) * [199.二叉树的右视图](https://leetcode.cn/problems/binary-tree-right-side-view/) -* [637.二叉树的层平均值](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [637.二叉树的层平均值](https://leetcode.cn/problems/average-of-levels-in-binary-tree/) * [429.N叉树的层序遍历](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) * [515.在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) * [116.填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 9db7409e04..edb5ea3ea7 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -133,7 +133,7 @@ left与right的逻辑处理; // 中 ![236.二叉树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020415105872.png) -就像图中一样直接返回7,多美滋滋。 +就像图中一样直接返回7。 但事实上还要遍历根节点右子树(即使此时已经找到了目标节点了),也就是图中的节点4、15、20。 diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index 4bee585ba8..5bdbcbc7e0 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -30,7 +30,7 @@ ### 暴力排序 -最直观的想法,莫过于:每个数平方之后,排个序,美滋滋,代码如下: +最直观的想法,莫过于:每个数平方之后,排个序,代码如下: ```CPP class Solution {