当前位置:网站首页>LeetCode 90. Subset II
LeetCode 90. Subset II
2022-07-02 06:10:00 【Great white sheep_ Aries】
Title Description
solution
We consider the method of backtracking , But and LeetCode 78. A subset of The difference is that we need to consider pruning , For example, for topic examples n u m s = [ 1 , 2 , 2 ] nums = [1,2,2] nums=[1,2,2], The results generated without pruning are shown in the following figure 
And the right result should be 
So we have to prune , In the code , It needs to be sorted first , Put the same elements together , If you find that n u m s [ i ] = = n u m s [ i − 1 ] nums[i] == nums[i-1] nums[i]==nums[i−1], Then skip . See the following implementation for details
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();
}
}
};
边栏推荐
- Generic classes and parameterized classes of SystemVerilog
- Ti millimeter wave radar learning (I)
- Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
- cookie插件和localForage离线储存插件
- Some experience of exercise and fitness
- LeetCode 77. 组合
- 脑与认知神经科学Matlab Psytoolbox认知科学实验设计——实验设计四
- 线性dp(拆分篇)
- Page printing plug-in print js
- 网络相关知识(硬件工程师)
猜你喜欢

TI毫米波雷达学习(一)

sudo提权

Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes

Web components series (VIII) -- custom component style settings

数据回放伴侣Rviz+plotjuggler

Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock

复杂 json数据 js前台解析 详细步骤《案例:一》

经典文献阅读之--SuMa++

Memcached installation

The official zero foundation introduction jetpack compose Chinese course is coming!
随机推荐
Mock simulate the background return data with mockjs
mock-用mockjs模拟后台返回数据
TI毫米波雷达学习(一)
WLAN相关知识点总结
Introduce uview into uni app
I/o impressions from readers | prize collection winners list
Stc8h8k series assembly and C51 actual combat - digital display ADC, key serial port reply key number and ADC value
Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
Mathematical statistics and machine learning
步骤详解 | 助您轻松提交 Google Play 数据安全表单
Sumo tutorial Hello World
让每一位开发者皆可使用机器学习技术
Page printing plug-in print js
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
Go 学习笔记整合
Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
从设计交付到开发,轻松畅快高效率!