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

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

class Solution {//because Elements in a subset must be in non-descending order.//so if we sort the vector first.//If we choose an elem[i] into subset, then we should choose elem[j](i
& S, int curPos, vector
& oneSubset, vector
>& allSubset) { allSubset.push_back(oneSubset); for (int i = curPos; i < S.size(); ++i) { if(i != curPos && S[i] == S[i-1]) continue; oneSubset.push_back(S[i]); DFS(S, i+1, oneSubset, allSubset); oneSubset.pop_back(); } }public: vector
> subsetsWithDup(vector
&S) { // Start typing your C/C++ solution below // DO NOT write int main() function sort(S.begin(), S.end()); vector
oneSubset; vector
> allSubset; DFS(S, 0, oneSubset, allSubset); return allSubset; }};

second time

class Solution {public:    void subsetUtil(vector
& S, vector
& used, int curIdx, vector
& curPath, vector
>& allPath) { if(curIdx == S.size()) { allPath.push_back(curPath); return ; } subsetUtil(S, used, curIdx+1, curPath, allPath); if(curIdx >= 1 && S[curIdx] == S[curIdx-1] && used[curIdx-1] == false) return; used[curIdx] = true; curPath.push_back(S[curIdx]); subsetUtil(S, used, curIdx+1, curPath, allPath); curPath.pop_back(); used[curIdx] = false; } vector
> subsetsWithDup(vector
&S) { // Start typing your C/C++ solution below // DO NOT write int main() function sort(S.begin(), S.end()); vector
used(S.size(), false); vector
> allPath; vector
curPath; subsetUtil(S, used, 0, curPath, allPath); return allPath; }};

转载地址:http://eqxti.baihongyu.com/

你可能感兴趣的文章
Single Number II --出现一次的数(重)
查看>>
Palindrome Partitioning --回文切割 深搜(重重)
查看>>
对话周鸿袆:从程序员创业谈起
查看>>
Mysql中下划线问题
查看>>
Xcode 11 报错,提示libstdc++.6 缺失,解决方案
查看>>
idea的安装以及简单使用
查看>>
Windows mysql 安装
查看>>
python循环语句与C语言的区别
查看>>
vue 项目中图片选择路径位置static 或 assets区别
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>
element-ui全局自定义主题
查看>>
facebook库runtime.js
查看>>
vue2.* 中 使用socket.io
查看>>
openlayers安装引用
查看>>
js报错显示subString/subStr is not a function
查看>>
高德地图js API实现鼠标悬浮于点标记时弹出信息窗体显示详情,点击点标记放大地图操作
查看>>
初始化VUE项目报错
查看>>
vue项目使用安装sass
查看>>
在osg场景中使用GLSL语言——一个例子
查看>>