当前位置:网站首页>LeetCode 90. 子集 II
LeetCode 90. 子集 II
2022-07-02 06:07:00 【大白羊_Aries】
题目描述
解法
我们考虑回溯的方法,但是和 LeetCode 78. 子集 不同的是我们这里需要考虑剪枝的问题,例如对于题目例子 n u m s = [ 1 , 2 , 2 ] nums = [1,2,2] nums=[1,2,2],不剪枝的话生成的结果如下图所示
而正确的结果应该是
所以我们必须有剪枝这一步,体现在代码上,需要先进行排序,让相同的元素靠在一起,如果发现 n u m s [ i ] = = n u m s [ i − 1 ] nums[i] == nums[i-1] nums[i]==nums[i−1],则跳过。具体看下面实现
class Solution {
public:
vector<vector<int>> res;
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<int> track;
backtrace(nums, 0, track);
return res;
}
void backtrace(vector<int>& nums, int start, vector<int> track)
{
res.push_back(track);
for (int i = start; i < nums.size(); i++)
{
if (i > start && nums[i] == nums[i - 1]) continue;
track.push_back(nums[i]);
backtrace(nums, i + 1, track);
track.pop_back();
}
}
};
边栏推荐
- 步骤详解 | 助您轻松提交 Google Play 数据安全表单
- 浏览器原理思维导图
- Mock simulate the background return data with mockjs
- Several keywords in C language
- JWT tool class
- Redis key value database [seckill]
- Generics and generic constraints of typescript
- Ti millimeter wave radar learning (I)
- Introduce uview into uni app
- Common websites for Postgraduates in data mining
猜你喜欢

网络相关知识(硬件工程师)

Classic literature reading -- deformable Detr

PHP development and testing WebService (soap) -win

Test case

Memcached installation

Reading classic literature -- Suma++

MySQL transaction and isolation level

From design delivery to development, easy and efficient!

How vite is compatible with lower version browsers

Sumo tutorial Hello World
随机推荐
I/o multiplexing & event driven yyds dry inventory
Test case
官方零基础入门 Jetpack Compose 的中文课程来啦!
线性dp(拆分篇)
PHP array to XML
Nacos 启动报错 Error creating bean with name ‘instanceOperatorClientImpl‘ defined in URL
Spark概述
The real definition of open source software
Can't the dist packaged by vite be opened directly in the browser
深度学习分类网络--VGGNet
PHP parent
格式校验js
On Web server
Deep learning classification network -- vggnet
The official zero foundation introduction jetpack compose Chinese course is coming!
如何使用MITMPROXy
PHP read file (read the specified line containing a string in the file)
Redis key value database [seckill]
Spark overview
在uni-app中引入uView