Leetcode Subsets
来源:互联网 发布:mac电脑的安卓模拟器 编辑:程序博客网 时间:2024/06/18 15:36
Leetcode Subsets 相关解决方案,本方法使用迭代完成算法,在leetcode上处理时间为4ms,以下提供相关代码以及测试:
#include<iostream>#include<vector>#include<algorithm>using namespace std;class Solution {public: vector<vector<int> > subsets(vector<int>& nums) { // Sort the nums for the left solution sort(nums.begin(), nums.end()); int len = nums.size(); // Init the result vector re vector<vector<int> > re; re.push_back(vector<int>()); // The pos vector saves the last element postion of the re[i] // in the nums. vector<int> pos; // The first element in re is empty, so the first value is -1 pos.push_back(-1); // The start and end position of the length k element in re int start = 0; int end = 0; while (re.back().size() != len) { // The elements of re with length k can be formed by the elements // of re with length k-1 append an element after its last element for (int i = start; i <= end; i ++) { for (int j = pos[i] + 1; j < len; j ++) { vector<int> temp(re[i]); temp.push_back(nums[j]); re.push_back(temp); pos.push_back(j); } } //Update the start and end position of elements with length k start = end + 1; end = re.size() - 1; } return re; }};int main(int argc, char* argv[]) { Solution so; vector<int> test; test.push_back(1); test.push_back(2); test.push_back(3); vector<vector<int> > re = so.subsets(test); for (int i = 0; i < re.size(); i ++) { for (int j = 0; j < re[i].size();j ++) { cout<<re[i][j]<<" "; } cout<<endl; } return 0;}
0 0
- 【LeetCode】 Subsets Subsets II
- 【LeetCode】Subsets & Subsets II
- LeetCode Subsets && Subsets II
- LeetCode Subsets && Subsets II
- LeetCode-Subsets & Subsets II
- LeetCode: Subsets
- LeetCode Subsets
- [Leetcode] Subsets
- LeetCode : Subsets
- [LeetCode] Subsets
- Leetcode: Subsets
- Leetcode: Subsets ||
- [leetcode] subsets
- Leetcode - Subsets
- [LeetCode]Subsets
- LeetCode-Subsets
- LeetCode - Subsets
- LeetCode | Subsets
- Oracle pl/sql编程 9---if语句
- hadoop学习笔记七 -- hadoop集群高可用架构安装配置
- Mac使用bootcamp安装windows双系统以及mac系统全新重装
- iOS中AutoLayer自动布局流程及相关方法
- PCA数据降维
- Leetcode Subsets
- [UVA 247]Calling Circles[传递闭包][floyd]
- NSIndexSet无符号整数集合
- MySQL 高可用:主从配置或主主(双主)配置
- RecyclerView设置Item点击事件
- 关于微博的正文的整个思维逻辑 (含有只有图片数量计算图片九宫格算法)
- 百度云下载速度慢解决方案(仅供参考,大神请指导)
- 黑马程序员---成长之路-----OC之基础篇MRC内存管理
- 归并排序(Merge Sort)