当前位置:网站首页>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();
}
}
};
边栏推荐
- From design delivery to development, easy and efficient!
- 穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
- The real definition of open source software
- 让每一位开发者皆可使用机器学习技术
- Reading classic literature -- Suma++
- Some descriptions of Mipi protocol of LCD
- 1035 Password
- Google play academy team PK competition, official start!
- 51单片机——ADC讲解(A/D转换、D/A转换)
- Cookie plugin and localforce offline storage plugin
猜你喜欢

Shenji Bailian 3.53-kruskal

AttributeError: ‘str‘ object has no attribute ‘decode‘

CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch

数据回放伴侣Rviz+plotjuggler

keepalived安装使用与快速入门

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

ZABBIX server trap command injection vulnerability (cve-2017-2824)

Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish

memcached安装

Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
随机推荐
Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
Page printing plug-in print js
Reading classic literature -- Suma++
JWT tool class
Keepalived installation, use and quick start
STC8H8K系列汇编和C51实战——按键允许按键计数(利用下降沿中断控制)
深度学习分类网络--Network in Network
AttributeError: ‘str‘ object has no attribute ‘decode‘
Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
Let every developer use machine learning technology
Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)
步骤详解 | 助您轻松提交 Google Play 数据安全表单
Detailed notes of ES6
51 single chip microcomputer - ADC explanation (a/d conversion, d/a conversion)
keepalived安装使用与快速入门
PHP read file (read JSON file, convert to array)
Shenji Bailian 3.54-dichotomy of dyeing judgment
From design delivery to development, easy and efficient!
数据回放伴侣Rviz+plotjuggler