SubsetsApr 18 '126226 / 16269
Given a set of distinct integers, 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,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
class Solution { public: vector<vector<int> > subsets(vector<int> &S) { sort(S.begin(), S.end()); vector<vector<int> > res; int n = S.size(); for (int i = 0; i <= n; i++) { vector<int> a; gen(res, S, i, 0, a); } return res; } void gen(vector<vector<int> >& res, const vector<int>& s, int n, int cur, vector<int> &a) { if (a.size() == n) { res.push_back(a); return; } if (cur == s.size()) return; gen(res, s, n, cur+1, a); a.push_back(s[cur]); gen(res, s, n, cur+1, a); a.pop_back(); } };
class Solution { public: vector<vector<int> > subsets(vector<int> &S) { sort(S.begin(), S.end()); vector<vector<int> > res; vector<int> a; gen(res, S, 0, a); return res; } void gen(vector<vector<int> >& res, const vector<int>& s, int cur, vector<int> &a) { if (cur == s.size()) { res.push_back(a); return; } gen(res, s, cur+1, a); a.push_back(s[cur]); gen(res, s, cur+1, a); a.pop_back(); } };
Subsets IIJun 25 '124769 / 13419
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 gen(vector<vector<int> >& res, const vector<int>& s, int cur, vector<int> &a) { if (cur == s.size()) { res.push_back(a); return; } int end = cur; while (end < s.size() && s[end] == s[cur]) end++; gen(res, s, end, a); for (int i = cur; i < end; i++) { a.push_back(s[cur]); gen(res, s, end, a); } while (end-- != cur) a.pop_back(); } vector<vector<int> > subsetsWithDup(vector<int> &S) { sort(S.begin(), S.end()); vector<vector<int> > res; vector<int> a; gen(res, S, 0, a); return res; } };
相关推荐
LeetCode:LeetCode培训&&分享
Solutions collection of LeetCode submissions in JavaScript & TypeScript (LeetCode 解题集之 JavaScript & TypeScript 版).zip
leetcode 分类 LeetCode LeetCode上的算法题使用golang和c++实现 目录结构 c++ LeetCode [^使用说明]:使用vs2019打开此路径下的sln文件,LeetCode.cpp中main函数用于测试 go src [^分包说明]:按照不同类型的题目建包...
leetcode 答案 LeetCode-practice 记录在leetcode练习的代码&总结 #1TwoSum #26RemoveDuplicatesFromSortedArray #27RemoveElement #35SearchInsertPosition 最佳答案未解 Git使用练习 练习下分支切换&合并 解决...
leetcode 分类 leetcode 练习 按照数据结构和算法分类对leetcode题型做出解答 主要使用C/C++语言 每道题给出算法思路,示例,分析时间、空间复杂度,争取做到最优解 暂时就想到这么多,以后再添加吧^_^
leetcodebook_leetcode 1~400知识点&题型总结&leetcode对应题表
leetcode和codewar JavaScript Data Structures & Algorithms 记录 JavaScript 数据结构和算法的一些学习笔记还有一些算法题 书籍 (不推荐阅读 ) 算法题 经验总结 数组提供的遍历方法没有 for 循环快 while 和 for ...
给出了《剑指offer》第2版中对应的LeetCode题目。 # 题目 题解 难度 1 Easy 2 Medium 3 Medium 4 Hard 5 Medium 6 Medium 7 Easy 8 Medium 9 Easy 10 Hard 11 Medium 12 Medium 13 Easy 14 Easy 15 Medium 16 Medium...
LeetCode 刷题汇总1
LeetCode 选讲1
1 二和 [C] 2 两个数相加 [C] 3 无重复字符的最长子串 [C] 4 两个排序数组的中位数 [C] 5 最长回文子串 [C] 6 之字折线转换 [C] 7 反转整数 [C] 10 正则表达式匹配 [C] 12 整数转罗马 [C] 13 罗马到整数 [C] 175 ...
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
用java刷leetcode mtLeetCode 用 java & scala 刷 leetcode
leetcode的C代码实现,目前只有1~50,后面还有零星几个,供参考。
leetcode 382 LeetCode && LineCode leetcode 进度 218 / 382 付费题不做 全部做对 微软 google 不是梦
leetcode题库 Leetcode leetcode及类似的算法&数据结构题记录 现在面试貌似都喜欢加一些,空闲之余用 C++ 随手刷一刷 有标准库实现的我会尽量使用标准库 如果大佬们有更高效的实现欢迎 讨论& PR !!!
2 leetcode 代码包含在文件中,以下链接为问答博客。 # Java C 困难 0001 简单的 0015 中等的 0018 中等的 0035 简单的 0049 中等的 0110 简单的 0149 难的 0172 简单的 0202 简单的 0204 简单的 0205 简单的 0217 ...
leetcode卡 SolutionForLearning Java&Leetcode 打卡算法和java多线程等一些基础问题
leetcode卡 leetcode 刷题打卡 python & java。
大佬的leetcode刷题笔记(c++版本)