From ee2993e60f2ac22f976f1780ef183f4eeb68a36b Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Sat, 9 Apr 2022 11:40:29 +0800 Subject: [PATCH 01/10] =?UTF-8?q?0332.=E9=87=8D=E6=96=B0=E5=AE=89=E6=8E=92?= =?UTF-8?q?=E8=A1=8C=E7=A8=8B=20=E6=B7=BB=E5=8A=A0Java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0332.重新安排行程.md | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md index 041a7f03..ceb64589 100644 --- a/problems/0332.重新安排行程.md +++ b/problems/0332.重新安排行程.md @@ -261,6 +261,43 @@ for (pairtarget : targets[result[result.size() - 1]]) ## 其他语言版本 ### java + +```java +class Solution { + private LinkedList res; + private LinkedList path = new LinkedList<>(); + + public List findItinerary(List> tickets) { + Collections.sort(tickets, (a, b) -> a.get(1).compareTo(b.get(1))); + path.add("JFK"); + boolean[] used = new boolean[tickets.size()]; + backTracking((ArrayList) tickets, used); + return res; + } + + public boolean backTracking(ArrayList> tickets, boolean[] used) { + if (path.size() == tickets.size() + 1) { + res = new LinkedList(path); + return true; + } + + for (int i = 0; i < tickets.size(); i++) { + if (!used[i] && tickets.get(i).get(0).equals(path.getLast())) { + path.add(tickets.get(i).get(1)); + used[i] = true; + + if (backTracking(tickets, used)) { + return true; + } + + used[i] = false; + path.removeLast(); + } + } + return false; + } +} +``` ```java class Solution { From b6633feb6996a46aa0858d73b1803370d5bafc8e Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Sun, 10 Apr 2022 10:27:02 +0800 Subject: [PATCH 02/10] =?UTF-8?q?0051.N=E7=9A=87=E5=90=8E=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4Java=E4=BB=A3=E7=A0=81=E5=92=8CPython=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E9=A1=BA=E5=BA=8F=EF=BC=8C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E4=B8=8E=E5=85=B6=E5=AE=83=E9=A2=98=E8=A7=A3=E9=A1=BA?= =?UTF-8?q?=E5=BA=8F=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0051.N皇后.md | 101 ++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 50 deletions(-) diff --git a/problems/0051.N皇后.md b/problems/0051.N皇后.md index 7eb0d7a0..1b0cf002 100644 --- a/problems/0051.N皇后.md +++ b/problems/0051.N皇后.md @@ -224,56 +224,6 @@ public: ## 其他语言补充 - -### Python - -```python -class Solution: - def solveNQueens(self, n: int) -> List[List[str]]: - if not n: return [] - board = [['.'] * n for _ in range(n)] - res = [] - def isVaild(board,row, col): - #判断同一列是否冲突 - for i in range(len(board)): - if board[i][col] == 'Q': - return False - # 判断左上角是否冲突 - i = row -1 - j = col -1 - while i>=0 and j>=0: - if board[i][j] == 'Q': - return False - i -= 1 - j -= 1 - # 判断右上角是否冲突 - i = row - 1 - j = col + 1 - while i>=0 and j < len(board): - if board[i][j] == 'Q': - return False - i -= 1 - j += 1 - return True - - def backtracking(board, row, n): - # 如果走到最后一行,说明已经找到一个解 - if row == n: - temp_res = [] - for temp in board: - temp_str = "".join(temp) - temp_res.append(temp_str) - res.append(temp_res) - for col in range(n): - if not isVaild(board, row, col): - continue - board[row][col] = 'Q' - backtracking(board, row+1, n) - board[row][col] = '.' - backtracking(board, 0, n) - return res -``` - ### Java ```java @@ -343,6 +293,55 @@ class Solution { } ``` +### Python + +```python +class Solution: + def solveNQueens(self, n: int) -> List[List[str]]: + if not n: return [] + board = [['.'] * n for _ in range(n)] + res = [] + def isVaild(board,row, col): + #判断同一列是否冲突 + for i in range(len(board)): + if board[i][col] == 'Q': + return False + # 判断左上角是否冲突 + i = row -1 + j = col -1 + while i>=0 and j>=0: + if board[i][j] == 'Q': + return False + i -= 1 + j -= 1 + # 判断右上角是否冲突 + i = row - 1 + j = col + 1 + while i>=0 and j < len(board): + if board[i][j] == 'Q': + return False + i -= 1 + j += 1 + return True + + def backtracking(board, row, n): + # 如果走到最后一行,说明已经找到一个解 + if row == n: + temp_res = [] + for temp in board: + temp_str = "".join(temp) + temp_res.append(temp_str) + res.append(temp_res) + for col in range(n): + if not isVaild(board, row, col): + continue + board[row][col] = 'Q' + backtracking(board, row+1, n) + board[row][col] = '.' + backtracking(board, 0, n) + return res +``` + ### Go ```Go @@ -398,6 +397,8 @@ func isValid(n, row, col int, chessboard [][]string) bool { return true } ``` + + ### Javascript ```Javascript var solveNQueens = function(n) { From 6a201720030faa7a70abcc313c1aa756245f3329 Mon Sep 17 00:00:00 2001 From: AronJudge <2286381138@qq.com> Date: Sat, 20 Aug 2022 10:38:58 +0800 Subject: [PATCH 03/10] =?UTF-8?q?463=20=E5=B2=9B=E5=B1=BF=E7=9A=84?= =?UTF-8?q?=E5=91=A8=E9=95=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新Java的方法二 --- problems/0463.岛屿的周长.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0463.岛屿的周长.md b/problems/0463.岛屿的周长.md index ea038140..3dc69f20 100644 --- a/problems/0463.岛屿的周长.md +++ b/problems/0463.岛屿的周长.md @@ -118,6 +118,27 @@ class Solution { return res; } } + +// 解法二 +class Solution { + public int islandPerimeter(int[][] grid) { + // 计算岛屿的周长 + // 方法二 : 遇到相邻的陆地总周长就-2 + int landSum = 0; // 陆地数量 + int cover = 0; // 相邻陆地数量 + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j] == 1) { + landSum++; + // 统计上面和左边的相邻陆地 + if(i - 1 >= 0 && grid[i-1][j] == 1) cover++; + if(j - 1 >= 0 && grid[i][j-1] == 1) cover++; + } + } + } + return landSum * 4 - cover * 2; + } +} ``` Python: From b2534f7993018e497670e05fdf258830a1f4b69b Mon Sep 17 00:00:00 2001 From: AronJudge <2286381138@qq.com> Date: Sat, 20 Aug 2022 16:50:29 +0800 Subject: [PATCH 04/10] =?UTF-8?q?05=20=E6=9C=80=E9=95=BF=E5=9B=9E=E6=96=87?= =?UTF-8?q?=E5=AD=90=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新Java动态绘画方法, 之前的根本就不是这个题 --- problems/0005.最长回文子串.md | 44 ++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index d53acf63..893525d2 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -263,21 +263,39 @@ public: Java: ```java -public int[] twoSum(int[] nums, int target) { - int[] res = new int[2]; - if(nums == null || nums.length == 0){ - return res; - } - Map map = new HashMap<>(); - for(int i = 0; i < nums.length; i++){ - int temp = target - nums[i]; - if(map.containsKey(temp)){ - res[1] = i; - res[0] = map.get(temp); +// 双指针 动态规划 +class Solution { + public String longestPalindrome(String s) { + if (s.length() == 0 || s.length() == 1) return s; + int length = 1; + int index = 0; + boolean[][] palindrome = new boolean[s.length()][s.length()]; + for (int i = 0; i < s.length(); i++) { + palindrome[i][i] = true; } - map.put(nums[i], i); + + for (int L = 2; L <= s.length(); L++) { + for (int i = 0; i < s.length(); i++) { + int j = i + L - 1; + if (j >= s.length()) break; + + if (s.charAt(i) != s.charAt(j)) { + palindrome[i][j] = false; + } else { + if (j - i < 3) { + palindrome[i][j] = true; + } else { + palindrome[i][j] = palindrome[i + 1][j - 1]; + } + } + if (palindrome[i][j] && j - i + 1 > length) { + length = j - i + 1; + index = i; + } + } + } + return s.substring(index, index + length); } - return res; } ``` From a97a7ef2ece2b73f87c196e01037508d1f197deb Mon Sep 17 00:00:00 2001 From: AronJudge <2286381138@qq.com> Date: Sat, 20 Aug 2022 16:50:29 +0800 Subject: [PATCH 05/10] =?UTF-8?q?05=20=E6=9C=80=E9=95=BF=E5=9B=9E=E6=96=87?= =?UTF-8?q?=E5=AD=90=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新Java动态规划方法 之前的根本就不是这个题 --- problems/0005.最长回文子串.md | 43 ++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index d53acf63..b9b568ab 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -263,21 +263,38 @@ public: Java: ```java -public int[] twoSum(int[] nums, int target) { - int[] res = new int[2]; - if(nums == null || nums.length == 0){ - return res; - } - Map map = new HashMap<>(); - for(int i = 0; i < nums.length; i++){ - int temp = target - nums[i]; - if(map.containsKey(temp)){ - res[1] = i; - res[0] = map.get(temp); +// 双指针 动态规划 +class Solution { + public String longestPalindrome(String s) { + if (s.length() == 0 || s.length() == 1) return s; + int length = 1; + int index = 0; + boolean[][] palindrome = new boolean[s.length()][s.length()]; + for (int i = 0; i < s.length(); i++) { + palindrome[i][i] = true; } - map.put(nums[i], i); + + for (int L = 2; L <= s.length(); L++) { + for (int i = 0; i < s.length(); i++) { + int j = i + L - 1; + if (j >= s.length()) break; + if (s.charAt(i) != s.charAt(j)) { + palindrome[i][j] = false; + } else { + if (j - i < 3) { + palindrome[i][j] = true; + } else { + palindrome[i][j] = palindrome[i + 1][j - 1]; + } + } + if (palindrome[i][j] && j - i + 1 > length) { + length = j - i + 1; + index = i; + } + } + } + return s.substring(index, index + length); } - return res; } ``` From 0c2f5c912cd628157e2e049e80abdd3c9984d8e5 Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Sun, 21 Aug 2022 09:27:35 +0800 Subject: [PATCH 06/10] =?UTF-8?q?0070.=E7=88=AC=E6=A5=BC=E6=A2=AFJavaScrip?= =?UTF-8?q?t=E4=BB=A3=E7=A0=81=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原代码貌似不是本题的哦 --- problems/0070.爬楼梯完全背包版本.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index ec019e57..28439e09 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -187,12 +187,14 @@ func climbStairs(n int) int { JavaScript: ```javascript var climbStairs = function(n) { - const dp = new Array(n+1).fill(0); - const weight = [1,2]; + const dp = new Array(n + 1).fill(0); + const m = 2; dp[0] = 1; - for(let i = 0; i <= n; i++){ //先遍历背包 - for(let j = 0; j < weight.length; j++){ // 再遍历物品 - if(i >= weight[j]) dp[i] += dp[i-weight[j]]; + for(let i = 1; i <= n; i++){ + for(let j = 1; j <= m; j++){ + if(i >= j) { + dp[i] += dp[i - j]; + } } } return dp[n]; From 7b61605475f39edb73e1ada48a61fe55e4bbea8f Mon Sep 17 00:00:00 2001 From: Jack_ZhijieFang <56966563+laerpeeK@users.noreply.github.com> Date: Mon, 22 Aug 2022 01:22:41 +0800 Subject: [PATCH 07/10] =?UTF-8?q?docs=200541.=E5=8F=8D=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E2=85=A1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改了该题描述,更新到leetcode最新问题描述。原先的问题描述,个人觉得对 "是否包括前2k个字符" 有理解上的困难。 --- problems/0541.反转字符串II.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index 26d0b89c..80692c0b 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -12,7 +12,7 @@ [力扣题目链接](https://leetcode.cn/problems/reverse-string-ii/) -给定一个字符串 s 和一个整数 k,你需要对从字符串开头算起的每隔 2k 个字符的前 k 个字符进行反转。 +给定一个字符串 s 和一个整数 k,从字符串开头算起, 每计数至 2k 个字符,就反转这 2k 个字符中的前 k 个字符。 如果剩余字符少于 k 个,则将剩余字符全部反转。 From 300922d68e3305024755fffb718f5d07a05b70cd Mon Sep 17 00:00:00 2001 From: Jamcy123 <1219502823@qq.com> Date: Tue, 23 Aug 2022 11:45:24 +0800 Subject: [PATCH 08/10] =?UTF-8?q?347.=E5=89=8DK=E4=B8=AA=E9=AB=98=E9=A2=91?= =?UTF-8?q?=E5=85=83=E7=B4=A0JS=E7=89=88=E6=9C=AC=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0347.前K个高频元素.md | 177 ++++++++++++++------------- 1 file changed, 90 insertions(+), 87 deletions(-) diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index 1d6a358b..d0a3063a 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -263,99 +263,102 @@ func topKFrequent(nums []int, k int) []int { -javaScript: +JavaScript: ```js -/** - * @param {number[]} nums - * @param {number} k - * @return {number[]} - */ -var topKFrequent = function(nums, k) { - const map = new Map(); - - for(const num of nums) { - map.set(num, (map.get(num) || 0) + 1); - } - - // 创建小顶堆 - const priorityQueue = new PriorityQueue((a, b) => a[1] - b[1]); - - // entry 是一个长度为2的数组,0位置存储key,1位置存储value - for (const entry of map.entries()) { - priorityQueue.push(entry); - if (priorityQueue.size() > k) { - priorityQueue.pop(); +// js 没有堆 需要自己构造 +class Heap { + constructor(compareFn) { + this.compareFn = compareFn; + this.queue = []; } - } - const ret = []; + // 添加 + push(item) { + // 推入元素 + this.queue.push(item); - for(let i = priorityQueue.size() - 1; i >= 0; i--) { - ret[i] = priorityQueue.pop()[0]; - } + // 上浮 + let index = this.size() - 1; // 记录推入元素下标 + let parent = Math.floor((index - 1) / 2); // 记录父节点下标 - return ret; + while (parent >= 0 && this.compare(parent, index) > 0) { // 注意compare参数顺序 + [this.queue[index], this.queue[parent]] = [this.queue[parent], this.queue[index]]; + + // 更新下标 + index = parent; + parent = Math.floor((index - 1) / 2); + } + } + + // 获取堆顶元素并移除 + pop() { + // 堆顶元素 + const out = this.queue[0]; + + // 移除堆顶元素 填入最后一个元素 + this.queue[0] = this.queue.pop(); + + // 下沉 + let index = 0; // 记录下沉元素下标 + let left = 1; // left 是左子节点下标 left + 1 则是右子节点下标 + let searchChild = this.compare(left, left + 1) > 0 ? left + 1 : left; + + while (searchChild !== undefined && this.compare(index, searchChild) > 0) { // 注意compare参数顺序 + [this.queue[index], this.queue[searchChild]] = [this.queue[searchChild], this.queue[index]]; + + // 更新下标 + index = searchChild; + left = 2 * index + 1; + searchChild = this.compare(left, left + 1) > 0 ? left + 1 : left; + } + + return out; + } + + size() { + return this.queue.length; + } + + // 使用传入的 compareFn 比较两个位置的元素 + compare(index1, index2) { + // 处理下标越界问题 + if (this.queue[index1] === undefined) return 1; + if (this.queue[index2] === undefined) return -1; + + return this.compareFn(this.queue[index1], this.queue[index2]); + } + +} + +const topKFrequent = function (nums, k) { + const map = new Map(); + + for (const num of nums) { + map.set(num, (map.get(num) || 0) + 1); + } + + // 创建小顶堆 + const heap= new Heap((a, b) => a[1] - b[1]); + + // entry 是一个长度为2的数组,0位置存储key,1位置存储value + for (const entry of map.entries()) { + heap.push(entry); + + if (heap.size() > k) { + heap.pop(); + } + } + + // return heap.queue.map(e => e[0]); + + const res = []; + + for (let i = heap.size() - 1; i >= 0; i--) { + res[i] = heap.pop()[0]; + } + + return res; }; - - -function PriorityQueue(compareFn) { - this.compareFn = compareFn; - this.queue = []; -} - -// 添加 -PriorityQueue.prototype.push = function(item) { - this.queue.push(item); - let index = this.queue.length - 1; - let parent = Math.floor((index - 1) / 2); - // 上浮 - while(parent >= 0 && this.compare(parent, index) > 0) { - // 交换 - [this.queue[index], this.queue[parent]] = [this.queue[parent], this.queue[index]]; - index = parent; - parent = Math.floor((index - 1) / 2); - } -} - -// 获取堆顶元素并移除 -PriorityQueue.prototype.pop = function() { - const ret = this.queue[0]; - - // 把最后一个节点移到堆顶 - this.queue[0] = this.queue.pop(); - - let index = 0; - // 左子节点下标,left + 1 就是右子节点下标 - let left = 1; - let selectedChild = this.compare(left, left + 1) > 0 ? left + 1 : left; - - // 下沉 - while(selectedChild !== undefined && this.compare(index, selectedChild) > 0) { - // 交换 - [this.queue[index], this.queue[selectedChild]] = [this.queue[selectedChild], this.queue[index]]; - index = selectedChild; - left = 2 * index + 1; - selectedChild = this.compare(left, left + 1) > 0 ? left + 1 : left; - } - - return ret; -} - -PriorityQueue.prototype.size = function() { - return this.queue.length; -} - -// 使用传入的 compareFn 比较两个位置的元素 -PriorityQueue.prototype.compare = function(index1, index2) { - if (this.queue[index1] === undefined) { - return 1; - } - if (this.queue[index2] === undefined) { - return -1; - } - - return this.compareFn(this.queue[index1], this.queue[index2]); -} ``` TypeScript: From 26cdabc43f91724bd41de2f03b2d157202f9f1d6 Mon Sep 17 00:00:00 2001 From: starry0819 Date: Tue, 23 Aug 2022 14:25:21 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200707.=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E9=93=BE=E8=A1=A8.md=20Java=E7=A4=BA=E4=BE=8B?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=83=A8=E5=88=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0707.设计链表.md 中 Java示例代码中,单链表的deleteAtIndex方法进行了修改。 --- problems/0707.设计链表.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 00886039..42912eda 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -353,8 +353,12 @@ class MyLinkedList { return; } size--; + if (index == 0) { + head = head.next; + return; + } ListNode pred = head; - for (int i = 0; i < index; i++) { + for (int i = 0; i < index - 1; i++) { pred = pred.next; } pred.next = pred.next.next; From ed199f1f75199ab9daf487c0c6b99d467f1cef43 Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Wed, 24 Aug 2022 21:31:30 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200077.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E4=BC=98=E5=8C=96.md=20cpp=E4=BB=A3=E7=A0=81=E8=AF=AD?= =?UTF-8?q?=E6=B3=95=E9=AB=98=E4=BA=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 网站本文cpp代码没有语法高亮,推测网站可以识别```CPP(网站其它cpp代码均用```CPP标识),但无法识别```c++ --- problems/0077.组合优化.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index e336fb75..b8b5f3c1 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -20,7 +20,7 @@ 大家先回忆一下[77. 组合]给出的回溯法的代码: -```c++ +```CPP class Solution { private: vector> result; // 存放符合条件结果的集合 @@ -52,7 +52,7 @@ public: 在遍历的过程中有如下代码: -```c++ +```CPP for (int i = startIndex; i <= n; i++) { path.push_back(i); backtracking(n, k, i + 1); @@ -76,7 +76,7 @@ for (int i = startIndex; i <= n; i++) { **如果for循环选择的起始位置之后的元素个数 已经不足 我们需要的元素个数了,那么就没有必要搜索了**。 注意代码中i,就是for循环里选择的起始位置。 -```c++ +```CPP for (int i = startIndex; i <= n; i++) { ``` @@ -98,13 +98,13 @@ for (int i = startIndex; i <= n; i++) { 所以优化之后的for循环是: -```c++ +```CPP for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) // i为本次搜索的起始位置 ``` 优化后整体代码如下: -```c++ +```CPP class Solution { private: vector> result;