mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
更新 0040.组合总和II 排版格式修复
This commit is contained in:
@ -41,13 +41,11 @@ candidates 中的每个数字在每个组合中只能使用一次。
|
|||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
# 算法公开课
|
## 算法公开课
|
||||||
|
|
||||||
**《代码随想录》算法视频公开课:[回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II](https://www.bilibili.com/video/BV12V4y1V73A),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II](https://www.bilibili.com/video/BV12V4y1V73A),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||||
|
|
||||||
|
## 思路
|
||||||
|
|
||||||
# 思路
|
|
||||||
|
|
||||||
|
|
||||||
这道题目和[39.组合总和](https://programmercarl.com/0039.组合总和.html)如下区别:
|
这道题目和[39.组合总和](https://programmercarl.com/0039.组合总和.html)如下区别:
|
||||||
@ -86,7 +84,7 @@ candidates 中的每个数字在每个组合中只能使用一次。
|
|||||||
|
|
||||||
可以看到图中,每个节点相对于 [39.组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)我多加了used数组,这个used数组下面会重点介绍。
|
可以看到图中,每个节点相对于 [39.组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)我多加了used数组,这个used数组下面会重点介绍。
|
||||||
|
|
||||||
## 回溯三部曲
|
### 回溯三部曲
|
||||||
|
|
||||||
* **递归函数参数**
|
* **递归函数参数**
|
||||||
|
|
||||||
@ -217,7 +215,7 @@ public:
|
|||||||
* 时间复杂度: O(n * 2^n)
|
* 时间复杂度: O(n * 2^n)
|
||||||
* 空间复杂度: O(n)
|
* 空间复杂度: O(n)
|
||||||
|
|
||||||
## 补充
|
### 补充
|
||||||
|
|
||||||
这里直接用startIndex来去重也是可以的, 就不用used数组了。
|
这里直接用startIndex来去重也是可以的, 就不用used数组了。
|
||||||
|
|
||||||
@ -257,7 +255,7 @@ public:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# 总结
|
## 总结
|
||||||
|
|
||||||
本题同样是求组合总和,但就是因为其数组candidates有重复元素,而要求不能有重复的组合,所以相对于[39.组合总和](https://programmercarl.com/0039.组合总和.html)难度提升了不少。
|
本题同样是求组合总和,但就是因为其数组candidates有重复元素,而要求不能有重复的组合,所以相对于[39.组合总和](https://programmercarl.com/0039.组合总和.html)难度提升了不少。
|
||||||
|
|
||||||
@ -265,14 +263,10 @@ public:
|
|||||||
|
|
||||||
所以Carl有必要把去重的这块彻彻底底的给大家讲清楚,**就连“树层去重”和“树枝去重”都是我自创的词汇,希望对大家理解有帮助!**
|
所以Carl有必要把去重的这块彻彻底底的给大家讲清楚,**就连“树层去重”和“树枝去重”都是我自创的词汇,希望对大家理解有帮助!**
|
||||||
|
|
||||||
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
|
### Java
|
||||||
|
|
||||||
# 其他语言版本
|
|
||||||
|
|
||||||
|
|
||||||
## Java
|
|
||||||
**使用标记数组**
|
**使用标记数组**
|
||||||
```Java
|
```Java
|
||||||
class Solution {
|
class Solution {
|
||||||
@ -355,7 +349,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Python
|
### Python
|
||||||
回溯
|
回溯
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
@ -442,7 +436,7 @@ class Solution:
|
|||||||
self.combinationSumHelper(candidates, target - candidates[i], i + 1, path, results)
|
self.combinationSumHelper(candidates, target - candidates[i], i + 1, path, results)
|
||||||
path.pop()
|
path.pop()
|
||||||
```
|
```
|
||||||
## Go
|
### Go
|
||||||
主要在于如何在回溯中去重
|
主要在于如何在回溯中去重
|
||||||
|
|
||||||
**使用used数组**
|
**使用used数组**
|
||||||
@ -518,7 +512,7 @@ func dfs(candidates []int, start int, target int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
## javaScript
|
### JavaScript
|
||||||
|
|
||||||
```js
|
```js
|
||||||
/**
|
/**
|
||||||
@ -588,7 +582,7 @@ var combinationSum2 = function(candidates, target) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
## TypeScript
|
### TypeScript
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function combinationSum2(candidates: number[], target: number): number[][] {
|
function combinationSum2(candidates: number[], target: number): number[][] {
|
||||||
@ -619,7 +613,7 @@ function combinationSum2(candidates: number[], target: number): number[][] {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
## Rust
|
### Rust
|
||||||
|
|
||||||
```Rust
|
```Rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
@ -654,7 +648,7 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## C
|
### C
|
||||||
|
|
||||||
```c
|
```c
|
||||||
int* path;
|
int* path;
|
||||||
@ -716,7 +710,7 @@ int** combinationSum2(int* candidates, int candidatesSize, int target, int* retu
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Swift
|
### Swift
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] {
|
func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] {
|
||||||
@ -749,7 +743,7 @@ func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Scala
|
### Scala
|
||||||
|
|
||||||
```scala
|
```scala
|
||||||
object Solution {
|
object Solution {
|
||||||
@ -784,3 +778,4 @@ object 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>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user