From c8ef172319234d388730cc835dbd9214c48ea82c Mon Sep 17 00:00:00 2001 From: Minxi Yan <77104028+Yan0613@users.noreply.github.com> Date: Mon, 20 May 2024 13:53:15 +0800 Subject: [PATCH 1/7] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0=E7=9B=B8?= =?UTF-8?q?=E5=8A=A0II.md=EF=BC=8C=E4=BF=AE=E6=94=B9=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0IIGo=E7=89=88=E6=9C=AC=E7=9A=84=E8=A7=A3?= =?UTF-8?q?=E7=AD=94=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改四数相加IIGo版本的解答代码,修改原版代码中的逻辑问题,使其通过leetcode的代码测试。 --- problems/0454.四数相加II.md | 36 +++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index db9a9e43..b73901f8 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -185,22 +185,28 @@ class Solution: ### Go: ```go -func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int { - m := make(map[int]int) //key:a+b的数值,value:a+b数值出现的次数 - count := 0 - // 遍历nums1和nums2数组,统计两个数组元素之和,和出现的次数,放到map中 +func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int { + m := make(map[int]int) + count := 0 + + // 构建nums1和nums2的和的map for _, v1 := range nums1 { - for _, v2 := range nums2 { - m[v1+v2]++ - } - } - // 遍历nums3和nums4数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来 - for _, v3 := range nums3 { - for _, v4 := range nums4 { - count += m[-v3-v4] - } - } - return count + for _, v2 := range nums2 { + m[v1+v2]++ + } + } + + // 遍历nums3和nums4,检查-c-d是否在map中 + for _, v3 := range nums3 { + for _, v4 := range nums4 { + sum := -v3 - v4 + if countVal, ok := m[sum]; ok { + count += countVal + } + } + } + + return count } ``` From 5073196a1083da5322dde47177f4af08ced9b4f1 Mon Sep 17 00:00:00 2001 From: iteng <1475208984@qq.com> Date: Sat, 25 May 2024 17:26:43 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E8=B0=83=E6=8D=A2leftNode=E3=80=81rightNod?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0101.对称二叉树.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 063b5429..31c24fc5 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -224,8 +224,8 @@ public: st.push(root->left); st.push(root->right); while (!st.empty()) { - TreeNode* leftNode = st.top(); st.pop(); TreeNode* rightNode = st.top(); st.pop(); + TreeNode* leftNode = st.top(); st.pop(); if (!leftNode && !rightNode) { continue; } @@ -950,3 +950,4 @@ public bool IsSymmetric(TreeNode root) + From 6ef77329949055714d0d263ff762e898ac38233d Mon Sep 17 00:00:00 2001 From: Nicolas Leigh Date: Sun, 26 May 2024 11:07:12 +0800 Subject: [PATCH 3/7] =?UTF-8?q?Add=20JavaScript=20solution=20to=20the=20pr?= =?UTF-8?q?oblem=201020.=E9=A3=9E=E5=9C=B0=E7=9A=84=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1020.飞地的数量.md | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/problems/1020.飞地的数量.md b/problems/1020.飞地的数量.md index 59610c68..5f1995a5 100644 --- a/problems/1020.飞地的数量.md +++ b/problems/1020.飞地的数量.md @@ -605,6 +605,63 @@ func bfs(grid [][]int, i, j int) { } ``` +### JavaScript + +```js +/** + * @param {number[][]} grid + * @return {number} + */ +var numEnclaves = function (grid) { + let row = grid.length; + let col = grid[0].length; + let count = 0; + + // Check the first and last row, if there is a 1, then change all the connected 1s to 0 and don't count them. + for (let j = 0; j < col; j++) { + if (grid[0][j] === 1) { + dfs(0, j, false); + } + if (grid[row - 1][j] === 1) { + dfs(row - 1, j, false); + } + } + + // Check the first and last column, if there is a 1, then change all the connected 1s to 0 and don't count them. + for (let i = 0; i < row; i++) { + if (grid[i][0] === 1) { + dfs(i, 0, false); + } + if (grid[i][col - 1] === 1) { + dfs(i, col - 1, false); + } + } + + // Check the rest of the grid, if there is a 1, then change all the connected 1s to 0 and count them. + for (let i = 1; i < row - 1; i++) { + for (let j = 1; j < col - 1; j++) { + dfs(i, j, true); + } + } + + function dfs(i, j, isCounting) { + let condition = i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] === 0; + + if (condition) return; + if (isCounting) count++; + + grid[i][j] = 0; + + dfs(i - 1, j, isCounting); + dfs(i + 1, j, isCounting); + dfs(i, j - 1, isCounting); + dfs(i, j + 1, isCounting); + } + + return count; +}; +``` + ### Rust dfs: @@ -700,3 +757,4 @@ impl Solution { + From 692f63a77e55a2b1141b4eac78634db306eb4ec9 Mon Sep 17 00:00:00 2001 From: alanx15a2 Date: Thu, 30 May 2024 12:00:12 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86=20add=20php=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的递归遍历.md | 56 ++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index a1d49e98..f2a97f4d 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -671,6 +671,62 @@ public void Traversal(TreeNode cur, IList res) } ``` +### PHP +```php +// 144.前序遍历 +function preorderTraversal($root) { + $output = []; + $this->traversal($root, $output); + return $output; +} + +function traversal($root, array &$output) { + if ($root->val === null) { + return; + } + + $output[] = $root->val; + $this->traversal($root->left, $output); + $this->traversal($root->right, $output); +} +``` +```php +// 94.中序遍历 +function inorderTraversal($root) { + $output = []; + $this->traversal($root, $output); + return $output; +} + +function traversal($root, array &$output) { + if ($root->val === null) { + return; + } + + $this->traversal($root->left, $output); + $output[] = $root->val; + $this->traversal($root->right, $output); +} +``` +```php +// 145.后序遍历 +function postorderTraversal($root) { + $output = []; + $this->traversal($root, $output); + return $output; +} + +function traversal($root, array &$output) { + if ($root->val === null) { + return; + } + + $this->traversal($root->left, $output); + $this->traversal($root->right, $output); + $output[] = $root->val; +} +``` +

From b5a5340d1a964254325dc4ed64507c3460a9d3b9 Mon Sep 17 00:00:00 2001 From: mengyi Date: Wed, 5 Jun 2024 19:08:31 -0400 Subject: [PATCH 5/7] add curly braces and comments to advoid confusion --- problems/0704.二分查找.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md index 5604cd56..d86146d6 100644 --- a/problems/0704.二分查找.md +++ b/problems/0704.二分查找.md @@ -174,13 +174,17 @@ class Solution { int left = 0, right = nums.length - 1; while (left <= right) { int mid = left + ((right - left) >> 1); - if (nums[mid] == target) + if (nums[mid] == target) { return mid; - else if (nums[mid] < target) + } + else if (nums[mid] < target) { left = mid + 1; - else if (nums[mid] > target) + } + else { // nums[mid] > target right = mid - 1; + } } + // 未找到目标值 return -1; } } @@ -194,13 +198,17 @@ class Solution { int left = 0, right = nums.length; while (left < right) { int mid = left + ((right - left) >> 1); - if (nums[mid] == target) + if (nums[mid] == target) { return mid; - else if (nums[mid] < target) + } + else if (nums[mid] < target) { left = mid + 1; - else if (nums[mid] > target) + } + else { // nums[mid] > target right = mid; + } } + // 未找到目标值 return -1; } } From 0f6ea64f68857ea7c080b820f31b88f622cab276 Mon Sep 17 00:00:00 2001 From: Jack Lin Date: Sat, 15 Jun 2024 15:11:24 +0800 Subject: [PATCH 6/7] Update 0242: Add C solution --- problems/0242.有效的字母异位词.md | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index ac03ddbb..61488f03 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -383,6 +383,31 @@ object Solution { } ``` +### C + +```c +bool isAnagram(char* s, char* t) { + int len1 = strlen(s), len2 = strlen(t); + if (len1 != len2) { + return false; + } + + int map1[26] = {0}, map2[26] = {0}; + for (int i = 0; i < len1; i++) { + map1[s[i] - 'a'] += 1; + map2[t[i] - 'a'] += 1; + } + + for (int i = 0; i < 26; i++) { + if (map1[i] != map2[i]) { + return false; + } + } + + return true; +} +``` + ## 相关题目 * [383.赎金信](https://programmercarl.com/0383.%E8%B5%8E%E9%87%91%E4%BF%A1.html) From 9aca3b5ca46a3bd39bbf9e15568b5bc390d55466 Mon Sep 17 00:00:00 2001 From: jycathy Date: Sun, 16 Jun 2024 17:32:38 +0800 Subject: [PATCH 7/7] fix prob.1207 --- problems/1207.独一无二的出现次数.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/problems/1207.独一无二的出现次数.md b/problems/1207.独一无二的出现次数.md index 5c5f92c3..cd89522e 100644 --- a/problems/1207.独一无二的出现次数.md +++ b/problems/1207.独一无二的出现次数.md @@ -40,9 +40,9 @@ 回归本题,**本题强调了-1000 <= arr[i] <= 1000**,那么就可以用数组来做哈希,arr[i]作为哈希表(数组)的下标,那么arr[i]可以是负数,怎么办?负数不能做数组下标。 -**此时可以定义一个2000大小的数组,例如int count[2002];**,统计的时候,将arr[i]统一加1000,这样就可以统计arr[i]的出现频率了。 +**此时可以定义一个2001大小的数组,例如int count[2001];**,统计的时候,将arr[i]统一加1000,这样就可以统计arr[i]的出现频率了。 -题目中要求的是是否有相同的频率出现,那么需要再定义一个哈希表(数组)用来记录频率是否重复出现过,bool fre[1002]; 定义布尔类型的就可以了,**因为题目中强调1 <= arr.length <= 1000,所以哈希表大小为1000就可以了**。 +题目中要求的是是否有相同的频率出现,那么需要再定义一个哈希表(数组)用来记录频率是否重复出现过,bool fre[1001]; 定义布尔类型的就可以了,**因为题目中强调1 <= arr.length <= 1000,所以哈希表大小为1000就可以了**。 如图所示: @@ -55,11 +55,11 @@ C++代码如下: class Solution { public: bool uniqueOccurrences(vector& arr) { - int count[2002] = {0}; // 统计数字出现的频率 + int count[2001] = {0}; // 统计数字出现的频率 for (int i = 0; i < arr.size(); i++) { count[arr[i] + 1000]++; } - bool fre[1002] = {false}; // 看相同频率是否重复出现 + bool fre[1001] = {false}; // 看相同频率是否重复出现 for (int i = 0; i <= 2000; i++) { if (count[i]) { if (fre[count[i]] == false) fre[count[i]] = true; @@ -78,7 +78,7 @@ public: ```java class Solution { public boolean uniqueOccurrences(int[] arr) { - int[] count = new int[2002]; + int[] count = new int[2001]; for (int i = 0; i < arr.length; i++) { count[arr[i] + 1000]++; // 防止负数作为下标 } @@ -103,10 +103,10 @@ class Solution { # 方法 1: 数组在哈西法的应用 class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: - count = [0] * 2002 + count = [0] * 2001 for i in range(len(arr)): count[arr[i] + 1000] += 1 # 防止负数作为下标 - freq = [False] * 1002 # 标记相同频率是否重复出现 + freq = [False] * 1001 # 标记相同频率是否重复出现 for i in range(2001): if count[i] > 0: if freq[count[i]] == False: @@ -139,12 +139,12 @@ class Solution: ``` javascript // 方法一:使用数组记录元素出现次数 var uniqueOccurrences = function(arr) { - const count = new Array(2002).fill(0);// -1000 <= arr[i] <= 1000 + const count = new Array(2001).fill(0);// -1000 <= arr[i] <= 1000 for(let i = 0; i < arr.length; i++){ count[arr[i] + 1000]++;// 防止负数作为下标 } // 标记相同频率是否重复出现 - const fre = new Array(1002).fill(false);// 1 <= arr.length <= 1000 + const fre = new Array(1001).fill(false);// 1 <= arr.length <= 1000 for(let i = 0; i <= 2000; i++){ if(count[i] > 0){//有i出现过 if(fre[count[i]] === false) fre[count[i]] = true;//之前未出现过,标记为出现