From 0a9653df8457535038888a57e05ba873109eccce Mon Sep 17 00:00:00 2001 From: Yukun J Date: Wed, 24 Aug 2022 11:22:43 -0400 Subject: [PATCH 1/7] =?UTF-8?q?Update:=200416=20=E4=BD=BF=E7=94=A8STL?= =?UTF-8?q?=E6=B1=82=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0416.分割等和子集.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 03eae8ef..9c90ea27 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -143,15 +143,12 @@ dp[j]的数值一定是小于等于j的。 class Solution { public: bool canPartition(vector& nums) { - int sum = 0; - + // 使用标准库函数 便捷求和 + int sum = accumulate(nums.begin(), nums.end(), 0); // dp[i]中的i表示背包内总和 // 题目中说:每个数组中的元素不会超过 100,数组的大小不会超过 200 // 总和不会大于20000,背包最大只需要其中一半,所以10001大小就可以了 vector dp(10001, 0); - for (int i = 0; i < nums.size(); i++) { - sum += nums[i]; - } if (sum % 2 == 1) return false; int target = sum / 2; From 55648dcb7905550ce30081295deee4de858fef35 Mon Sep 17 00:00:00 2001 From: SwaggyP <1352164869@qq.com> Date: Sun, 28 Aug 2022 10:26:45 +0800 Subject: [PATCH 2/7] =?UTF-8?q?0127.=E5=8D=95=E8=AF=8D=E6=8E=A5=E9=BE=99?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0go=E8=AF=AD=E8=A8=80=E7=9A=84=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0127.单词接龙 增加go语言的解法 --- problems/0127.单词接龙.md | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/problems/0127.单词接龙.md b/problems/0127.单词接龙.md index f1c6f182..6d719691 100644 --- a/problems/0127.单词接龙.md +++ b/problems/0127.单词接龙.md @@ -158,6 +158,57 @@ class Solution: return 0 ``` ## Go +```go +func ladderLength(beginWord string, endWord string, wordList []string) int { + wordMap, que, depth := getWordMap(wordList, beginWord), []string{beginWord}, 0 + for len(que) > 0 { + depth++ + qLen := len(que) // 单词的长度 + for i := 0; i < qLen; i++ { + word := que[0] + que = que[1:] // 首位单词出队 + candidates := getCandidates(word) + for _, candidate := range candidates { + if _, exist := wordMap[candidate]; exist { // 用生成的结果集去查询 + if candidate == endWord { + return depth + 1 + } + delete(wordMap, candidate) // 删除集合中的用过的结果 + que = append(que, candidate) + } + } + } + } + return 0 +} + + +// 获取单词Map为后续的查询增加速度 +func getWordMap(wordList []string, beginWord string) map[string]int { + wordMap := make(map[string]int) + for i, word := range wordList { + if _, exist := wordMap[word]; !exist { + if word != beginWord { + wordMap[word] = i + } + } + } + return wordMap +} + +// 用26个英文字母分别替换掉各个位置的字母,生成一个结果集 +func getCandidates(word string) []string { + var res []string + for i := 0; i < 26; i++ { + for j := 0; j < len(word); j++ { + if word[j] != byte(int('a')+i) { + res = append(res, word[:j]+string(int('a')+i)+word[j+1:]) + } + } + } + return res +} +``` ## JavaScript ```javascript From bc8030b45a7e2a6cdefe8944cb624d4fa1f7d1a1 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sun, 28 Aug 2022 14:31:18 +0800 Subject: [PATCH 3/7] =?UTF-8?q?Update=200347.=E5=89=8DK=E4=B8=AA=E9=AB=98?= =?UTF-8?q?=E9=A2=91=E5=85=83=E7=B4=A0.md=EF=BC=8C=E6=96=B0=E5=A2=9ECompar?= =?UTF-8?q?ator=E6=8E=A5=E5=8F=A3=E8=AF=B4=E6=98=8E=E5=8F=8A=E5=A4=A7?= =?UTF-8?q?=E9=A1=B6=E5=A0=86=E3=80=81=E5=B0=8F=E9=A1=B6=E5=A0=86=E4=B8=A4?= =?UTF-8?q?=E7=A7=8Djava=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0347.前K个高频元素.md | 59 +++++++++++++++++++++------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index d4059b9b..8256e629 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -140,24 +140,55 @@ public: Java: ```java +/*Comparator接口说明: + * 返回负数,形参中第一个参数排在前面;返回正数,形参中第二个参数排在前面 + * 对于队列:排在前面意味着往队头靠 + * 对于堆(使用PriorityQueue实现):从队头到队尾按从小到大排就是最小堆(小顶堆), + * 从队头到队尾按从大到小排就是最大堆(大顶堆)--->队头元素相当于堆的根节点 + * */ class Solution { - public int[] topKFrequent(int[] nums, int k) { - int[] result = new int[k]; - HashMap map = new HashMap<>(); - for (int num : nums) { - map.put(num, map.getOrDefault(num, 0) + 1); + //解法1:基于大顶堆实现 + public int[] topKFrequent1(int[] nums, int k) { + Map map = new HashMap<>();//key为数组元素值,val为对应出现次数 + for(int num:nums){ + map.put(num,map.getOrDefault(num,0)+1); } - - Set> entries = map.entrySet(); - // 根据map的value值,构建于一个大顶堆(o1 - o2: 小顶堆, o2 - o1 : 大顶堆) - PriorityQueue> queue = new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue()); - for (Map.Entry entry : entries) { - queue.offer(entry); + //在优先队列中存储二元组(num,cnt),cnt表示元素值num在数组中的出现次数 + //出现次数按从队头到队尾的顺序是从大到小排,出现次数最多的在队头(相当于大顶堆) + PriorityQueue pq = new PriorityQueue<>((pair1, pair2)->pair2[1]-pair1[1]); + for(Map.Entry entry:map.entrySet()){//大顶堆需要对所有元素进行排序 + pq.add(new int[]{entry.getKey(),entry.getValue()}); } - for (int i = k - 1; i >= 0; i--) { - result[i] = queue.poll().getKey(); + int[] ans = new int[k]; + for(int i=0;i map = new HashMap<>();//key为数组元素值,val为对应出现次数 + for(int num:nums){ + map.put(num,map.getOrDefault(num,0)+1); + } + //在优先队列中存储二元组(num,cnt),cnt表示元素值num在数组中的出现次数 + //出现次数按从队头到队尾的顺序是从小到大排,出现次数最低的在队头(相当于小顶堆) + PriorityQueue pq = new PriorityQueue<>((pair1,pair2)->pair1[1]-pair2[1]); + for(Map.Entry entry:map.entrySet()){//小顶堆只需要维持k个元素有序 + if(pq.size()pq.peek()[1]){//当前元素出现次数大于小顶堆的根结点(这k个元素中出现次数最少的那个) + pq.poll();//弹出队头(小顶堆的根结点),即把堆里出现次数最少的那个删除,留下的就是出现次数多的了 + pq.add(new int[]{entry.getKey(),entry.getValue()}); + } + } + } + int[] ans = new int[k]; + for(int i=k-1;i>=0;i--){//依次弹出小顶堆,先弹出的是堆的根,出现次数少,后面弹出的出现次数多 + ans[i] = pq.poll()[0]; + } + return ans; } } ``` From 39f80d5bc38868f70a85d865bba9a20fc4965cf3 Mon Sep 17 00:00:00 2001 From: Xin-Sheng-5075 <2061461846@qq.com> Date: Sun, 28 Aug 2022 19:13:24 +0800 Subject: [PATCH 4/7] Signed-off-by: Xin-Sheng-5075 <2061461846@qq.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加题目 200.岛屿数量 的Java版本 --- problems/0200.岛屿数量.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0200.岛屿数量.md b/problems/0200.岛屿数量.md index b88e5fd2..59c4e566 100644 --- a/problems/0200.岛屿数量.md +++ b/problems/0200.岛屿数量.md @@ -247,3 +247,35 @@ public: ## 其他语言版本 + +### Java + +下面的代码使用的是深度优先搜索 DFS 的做法。为了统计岛屿数量同时不重复记录,每当我们搜索到一个岛后,就将这个岛 “淹没” —— 将这个岛所占的地方从 “1” 改为 “0”,这样就不用担心后续会重复记录这个岛屿了。而 DFS 的过程就体现在 “淹没” 这一步中。详见代码: + +```java +public int numIslands(char[][] grid) { + int res = 0; //记录找到的岛屿数量 + for(int i = 0;i < grid.length;i++){ + for(int j = 0;j < grid[0].length;j++){ + //找到“1”,res加一,同时淹没这个岛 + if(grid[i][j] == '1'){ + res++; + dfs(grid,i,j); + } + } + } + return res; +} +//使用DFS“淹没”岛屿 +public void dfs(char[][] grid, int i, int j){ + //搜索边界:索引越界或遍历到了"0" + if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == '0') return; + //将这块土地标记为"0" + grid[i][j] = '0'; + //根据"每座岛屿只能由水平方向或竖直方向上相邻的陆地连接形成",对上下左右的相邻顶点进行dfs + dfs(grid,i - 1,j); + dfs(grid,i + 1,j); + dfs(grid,i,j + 1); + dfs(grid,i,j - 1); +} +``` \ No newline at end of file From d91233d0443753b747d139e00b081fa4f4c39fb1 Mon Sep 17 00:00:00 2001 From: huaqi <2931246464@qq.com> Date: Tue, 30 Aug 2022 21:18:16 +0800 Subject: [PATCH 5/7] =?UTF-8?q?0977.=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E7=9A=84=E5=B9=B3=E6=96=B9-typescript=E7=89=88=E6=9C=AC-?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0977.有序数组的平方.md | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index eb9f42b1..501b9a42 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -239,18 +239,24 @@ Typescript: ```typescript function sortedSquares(nums: number[]): number[] { - let left: number = 0, right: number = nums.length - 1; - let resArr: number[] = new Array(nums.length); - let resArrIndex: number = resArr.length - 1; + const ans: number[] = []; + let left = 0, + right = nums.length - 1; + while (left <= right) { - if (Math.abs(nums[left]) < Math.abs(nums[right])) { - resArr[resArrIndex] = nums[right--] ** 2; + // 右侧的元素不需要取绝对值,nums 为非递减排序的整数数组 + // 在同为负数的情况下,左侧的平方值一定大于右侧的平方值 + if (Math.abs(nums[left]) > nums[right]) { + // 使用 Array.prototype.unshift() 直接在数组的首项插入当前最大值 + ans.unshift(nums[left] ** 2); + left++; } else { - resArr[resArrIndex] = nums[left++] ** 2; + ans.unshift(nums[right] ** 2); + right--; } - resArrIndex--; } - return resArr; + + return ans; }; ``` From 3f96750bece9167e25e52a61c1f52cdcdecdb8ea Mon Sep 17 00:00:00 2001 From: Wen Date: Tue, 30 Aug 2022 22:30:25 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E4=BC=98=E5=8C=96=20242.=20=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?= =?UTF-8?q?=20Java=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 87302482..d54285eb 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -92,18 +92,24 @@ public: Java: ```java +/** + * 242. 有效的字母异位词 字典解法 + * 时间复杂度O(m+n) 空间复杂度O(1) + */ class Solution { public boolean isAnagram(String s, String t) { - int[] record = new int[26]; - for (char c : s.toCharArray()) { - record[c - 'a'] += 1; + + for (int i = 0; i < s.length(); i++) { + record[s.charAt(i) - 'a']++; } - for (char c : t.toCharArray()) { - record[c - 'a'] -= 1; + + for (int i = 0; i < t.length(); i++) { + record[t.charAt(i) - 'a']--; } - for (int i : record) { - if (i != 0) { + + for (int count: record) { + if (count != 0) { return false; } } From a275c2cc553ad35e50d736e0630947a5bdf2d13e Mon Sep 17 00:00:00 2001 From: YukunJ Date: Tue, 30 Aug 2022 21:51:30 -0400 Subject: [PATCH 7/7] =?UTF-8?q?Update:=20=E6=B3=A8=E9=87=8A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=BD=BF=E7=94=A8STL=E6=B1=82=E5=92=8C=E7=9A=84?= =?UTF-8?q?=E7=AE=80=E4=BE=BF=E5=B0=8F=E8=B4=B4=E5=A3=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0416.分割等和子集.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 9c90ea27..fa677da0 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -143,12 +143,17 @@ dp[j]的数值一定是小于等于j的。 class Solution { public: bool canPartition(vector& nums) { - // 使用标准库函数 便捷求和 - int sum = accumulate(nums.begin(), nums.end(), 0); + int sum = 0; + // dp[i]中的i表示背包内总和 // 题目中说:每个数组中的元素不会超过 100,数组的大小不会超过 200 // 总和不会大于20000,背包最大只需要其中一半,所以10001大小就可以了 vector dp(10001, 0); + for (int i = 0; i < nums.size(); i++) { + sum += nums[i]; + } + // 也可以使用库函数一步求和 + // int sum = accumulate(nums.begin(), nums.end(), 0); if (sum % 2 == 1) return false; int target = sum / 2;