当前位置:网站首页>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();
}
}
};
边栏推荐
- Some descriptions of Mipi protocol of LCD
- 492. Construction rectangle
- Some experience of exercise and fitness
- MySQL transaction and isolation level
- Spark overview
- From design delivery to development, easy and efficient!
- 来自读者们的 I/O 观后感|有奖征集获奖名单
- 浏览器原理思维导图
- Little bear sect manual query and ADC in-depth study
- Lambda 表达式 和 方法引用
猜你喜欢
Redis key value database [primary]
memcached安装
Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
复杂 json数据 js前台解析 详细步骤《案例:一》
Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)
ES6的详细注解
Step by step | help you easily submit Google play data security form
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
深度学习分类网络--Network in Network
社区说|Kotlin Flow 的原理与设计哲学
随机推荐
Eco express micro engine system has supported one click deployment to cloud hosting
Ti millimeter wave radar learning (I)
Servlet web XML configuration details (3.0)
Google Play Academy 组队 PK 赛,正式开赛!
JWT tool class
使用sha256文件验证下载的文件
[C language] screening method for prime numbers
Page printing plug-in print js
Redis key value database [primary]
Redis Key-Value数据库 【秒杀】
Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
Detailed steps of JS foreground parsing of complex JSON data "case: I"
Problems encountered in uni app development (continuous update)
STC8H8K系列汇编和C51实战——按键允许按键计数(利用下降沿中断控制)
Deep learning classification network -- vggnet
From design delivery to development, easy and efficient!
The official zero foundation introduction jetpack compose Chinese course is coming!
Detailed notes of ES6
PHP obtains some values in the string according to the specified characters, and reorganizes the remaining strings into a new array
线性dp(拆分篇)