当前位置:网站首页>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();
}
}
};
边栏推荐
- 495. Timo attack
- Spark overview
- 复杂 json数据 js前台解析 详细步骤《案例:一》
- Current situation analysis of Devops and noops
- ROS create workspace
- Stc8h8k Series Assembly and c51 Real combat - NIXIE TUBE displays ADC, Key Series port reply Key number and ADC value
- STC8H8K系列汇编和C51实战——串口发送菜单界面选择不同功能
- 使用HBuilderX的一些常用功能
- From design delivery to development, easy and efficient!
- The real definition of open source software
猜你喜欢
数据回放伴侣Rviz+plotjuggler
Shenji Bailian 3.54-dichotomy of dyeing judgment
uni-app开发中遇到的问题(持续更新)
How vite is compatible with lower version browsers
Shenji Bailian 3.53-kruskal
Detailed steps of JS foreground parsing of complex JSON data "case: I"
Ros2 --- lifecycle node summary
CNN可视化技术 -- CAM & Grad-CAM详解及pytorch简洁实现
WLAN相关知识点总结
Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
随机推荐
Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
数据回放伴侣Rviz+plotjuggler
Little bear sect manual query and ADC in-depth study
51 single chip microcomputer - ADC explanation (a/d conversion, d/a conversion)
Redis Key-Value数据库 【高级】
网络相关知识(硬件工程师)
LeetCode 283. 移动零
CNN可视化技术 -- CAM & Grad-CAM详解及pytorch简洁实现
sudo提权
500. 键盘行
Zhuanzhuanben - LAN construction - Notes
BGP 路由優選規則和通告原則
Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)
LeetCode 83. 删除排序链表中的重复元素
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
Servlet web XML configuration details (3.0)
Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)
LeetCode 39. 组合总和
锐捷EBGP 配置案例
Picture clipping plug-in cropper js