Merge pull request #1678 from initialqing/master

Feat:0090.子集Ⅱ,增加TypeScript的set去重版本题解。
This commit is contained in:
程序员Carl
2022-10-09 10:26:36 +08:00
committed by GitHub

View File

@ -367,6 +367,39 @@ function subsetsWithDup(nums: number[]): number[][] {
};
```
set去重版本:
```typescript
// 使用set去重版本
function subsetsWithDup(nums: number[]): number[][] {
const result: number[][] = [];
const path: number[] = [];
// 去重之前先排序
nums.sort((a, b) => a - b);
function backTracking(startIndex: number) {
// 收集结果
result.push([...path])
// 此处不返回也可以因为每次递归都会使startIndex + 1当这个数大到nums.length的时候就不会进入递归了。
if (startIndex === nums.length) {
return
}
// 定义每一个树层的set集合
const set: Set<number> = new Set()
for (let i = startIndex; i < nums.length; i++) {
// 去重
if (set.has(nums[i])) {
continue
}
set.add(nums[i])
path.push(nums[i])
backTracking(i + 1)
// 回溯
path.pop()
}
}
backTracking(0)
return result
};
```
### Rust
```Rust