Merge branch 'master' of github.com:youngyangyang04/leetcode-master

This commit is contained in:
programmercarl
2024-05-26 10:23:27 +08:00
3 changed files with 12 additions and 5 deletions

View File

@ -379,6 +379,8 @@ class Solution {
String targetLocation; String targetLocation;
//遍历从当前位置出发的机票 //遍历从当前位置出发的机票
for (int i = 0; i < targetLocations.size(); i++) { for (int i = 0; i < targetLocations.size(); i++) {
//去重,否则在最后一个测试用例中遇到循环时会无限递归
if(i > 0 && targetLocations.get(i).equals(targetLocations.get(i - 1))) continue;
targetLocation = targetLocations.get(i); targetLocation = targetLocations.get(i);
//删除终点列表中当前的终点 //删除终点列表中当前的终点
targetLocations.remove(i); targetLocations.remove(i);
@ -419,7 +421,6 @@ class Solution {
``` ```
### Python ### Python
```
回溯 使用字典 回溯 使用字典
```python ```python
class Solution: class Solution:

View File

@ -405,6 +405,11 @@ class Heap {
// 获取堆顶元素并移除 // 获取堆顶元素并移除
pop() { pop() {
// 边界情况,只有一个元素或没有元素应直接弹出
if (this.size() <= 1) {
return this.queue.pop()
}
// 堆顶元素 // 堆顶元素
const out = this.queue[0]; const out = this.queue[0];
@ -416,7 +421,7 @@ class Heap {
let left = 1; // left 是左子节点下标 left + 1 则是右子节点下标 let left = 1; // left 是左子节点下标 left + 1 则是右子节点下标
let searchChild = this.compare(left, left + 1) > 0 ? left + 1 : left; 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]]; [this.queue[index], this.queue[searchChild]] = [this.queue[searchChild], this.queue[index]];
// 更新下标 // 更新下标
@ -608,3 +613,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>

View File

@ -173,9 +173,9 @@ dp[j] 表示填满j包括j这么大容积的包有dp[j]种方法
* 已经有一个1nums[i] 的话,有 dp[4]种方法 凑成 容量为5的背包。 * 已经有一个1nums[i] 的话,有 dp[4]种方法 凑成 容量为5的背包。
* 已经有一个2nums[i] 的话,有 dp[3]种方法 凑成 容量为5的背包。 * 已经有一个2nums[i] 的话,有 dp[3]种方法 凑成 容量为5的背包。
* 已经有一个3nums[i] 的话,有 dp[2]方法 凑成 容量为5的背包 * 已经有一个3nums[i] 的话,有 dp[2]方法 凑成 容量为5的背包
* 已经有一个4nums[i] 的话,有 dp[1]方法 凑成 容量为5的背包 * 已经有一个4nums[i] 的话,有 dp[1]方法 凑成 容量为5的背包
* 已经有一个5 nums[i])的话,有 dp[0]方法 凑成 容量为5的背包 * 已经有一个5 nums[i])的话,有 dp[0]方法 凑成 容量为5的背包
那么凑整dp[5]有多少方法呢,也就是把 所有的 dp[j - nums[i]] 累加起来。 那么凑整dp[5]有多少方法呢,也就是把 所有的 dp[j - nums[i]] 累加起来。