当前位置:网站首页>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();
}
}
};
边栏推荐
- Flutter hybrid development: develop a simple quick start framework | developers say · dtalk
- Regular expression summary
- JWT工具类
- LeetCode 40. 组合总和 II
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
- 线性dp(拆分篇)
- 页面打印插件print.js
- Scheme and implementation of automatic renewal of token expiration
- 使用sha256文件验证下载的文件
- Redis key value database [advanced]
猜你喜欢
随机推荐
Go 学习笔记整合
Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
Little bear sect manual query and ADC in-depth study
495. Timo attack
Stc8h8k Series Assembly and c51 Real combat - NIXIE TUBE displays ADC, Key Series port reply Key number and ADC value
Eco express micro engine system has supported one click deployment to cloud hosting
Deep learning classification network -- Network in network
Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
Some experience of exercise and fitness
CNN可视化技术 -- CAM & Grad-CAM详解及pytorch简洁实现
Use some common functions of hbuilderx
ZABBIX server trap command injection vulnerability (cve-2017-2824)
从设计交付到开发,轻松畅快高效率!
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
Spark overview
Lambda expressions and method references
492. Construction rectangle
Zhuanzhuanben - LAN construction - Notes
cookie插件和localForage离线储存插件
LeetCode 83. 删除排序链表中的重复元素