博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Subsets II
阅读量:4070 次
发布时间:2019-05-25

本文共 1043 字,大约阅读时间需要 3 分钟。

Given a collection of integers that might contain duplicates, S, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

For example,
If S = [1,2,2], a solution is:

[  [2],  [1],  [1,2,2],  [2,2],  [1,2],  []]

有图为证:

class Solution {public:    void subReII(vector
>& re, vector
&s,int j) { if(s.size() <= j) return; int size_ = re.size(); int k = j + 1; //get the 重复区间,k是下个不重复的位置。 while(k < s.size() && s[k] == s[k - 1]) k++; for(int i = 0; i < size_; ++i) { int cur = j; vector
copy(re[i]); //将重复的元素从 1 个到所有依次加入进去。 while(cur < k) { copy.push_back(s[j]); re.push_back(copy); ++cur; } } //skip the 重复区间,到下一个不是重复的位置,递归。 subReII(re, s, k); } vector
> subsetsWithDup(vector
&S) { vector
> re; vector
sol; sort(S.begin(),S.end()); re.push_back(sol); subReII(re, S, 0); return re; }};

你可能感兴趣的文章
gdb debug tips
查看>>
linux和windows内存布局验证
查看>>
本地服务方式搭建etcd集群
查看>>
安装k8s Master高可用集群
查看>>
忽略图片透明区域的事件(Flex)
查看>>
Xpath使用方法
查看>>
移动端自动化测试-Mac-IOS-Appium环境搭建
查看>>
Selenium之前世今生
查看>>
Selenium-WebDriverApi接口详解
查看>>
Selenium-ActionChains Api接口详解
查看>>
Selenium-Switch与SelectApi接口详解
查看>>
Selenium-Css Selector使用方法
查看>>
Linux常用统计命令之wc
查看>>
Java.nio
查看>>
PHP那点小事--三元运算符
查看>>
fastcgi_param 详解
查看>>
Linux中的进程
查看>>
学习python(1)——环境与常识
查看>>
学习设计模式(3)——单例模式和类的成员函数中的静态变量的作用域
查看>>
一文看清HBase的使用场景
查看>>