diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md index 144672a9..a34490ea 100644 --- a/problems/0332.重新安排行程.md +++ b/problems/0332.重新安排行程.md @@ -379,6 +379,8 @@ class Solution { String targetLocation; //遍历从当前位置出发的机票 for (int i = 0; i < targetLocations.size(); i++) { + //去重,否则在最后一个测试用例中遇到循环时会无限递归 + if(i > 0 && targetLocations.get(i).equals(targetLocations.get(i - 1))) continue; targetLocation = targetLocations.get(i); //删除终点列表中当前的终点 targetLocations.remove(i); @@ -419,7 +421,6 @@ class Solution { ``` ### Python -``` 回溯 使用字典 ```python class Solution: diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index 34d9f82c..cca9b0ed 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -405,6 +405,11 @@ class Heap { // 获取堆顶元素并移除 pop() { + // 边界情况,只有一个元素或没有元素应直接弹出 + if (this.size() <= 1) { + return this.queue.pop() + } + // 堆顶元素 const out = this.queue[0]; @@ -416,7 +421,7 @@ class Heap { 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参数顺序 + while (this.compare(index, searchChild) > 0) { // 注意compare参数顺序 [this.queue[index], this.queue[searchChild]] = [this.queue[searchChild], this.queue[index]]; // 更新下标 @@ -608,3 +613,4 @@ impl Solution { + diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 677c2856..92616ed1 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -173,9 +173,9 @@ dp[j] 表示:填满j(包括j)这么大容积的包,有dp[j]种方法 * 已经有一个1(nums[i]) 的话,有 dp[4]种方法 凑成 容量为5的背包。 * 已经有一个2(nums[i]) 的话,有 dp[3]种方法 凑成 容量为5的背包。 -* 已经有一个3(nums[i]) 的话,有 dp[2]中方法 凑成 容量为5的背包 -* 已经有一个4(nums[i]) 的话,有 dp[1]中方法 凑成 容量为5的背包 -* 已经有一个5 (nums[i])的话,有 dp[0]中方法 凑成 容量为5的背包 +* 已经有一个3(nums[i]) 的话,有 dp[2]种方法 凑成 容量为5的背包 +* 已经有一个4(nums[i]) 的话,有 dp[1]种方法 凑成 容量为5的背包 +* 已经有一个5 (nums[i])的话,有 dp[0]种方法 凑成 容量为5的背包 那么凑整dp[5]有多少方法呢,也就是把 所有的 dp[j - nums[i]] 累加起来。