leetcode: Permutations
来源:互联网 发布:adobe acrobat软件下载 编辑:程序博客网 时间:2024/05/21 07:50
方法1:
class Solution {public: vector<vector<int> > permute(vector<int> &num) { vector< vector< int> > res; permutation( res, num, 0); return res; } void permutation( vector< vector< int> > &res, vector< int> &num, int i){ if( i == num.size()){ res.push_back(num); return; } for( int j = i; j < num.size(); ++j){ swap( num[i], num[j]); permutation( res, num, i+1); swap( num[j], num[i]); } } void swap( int &a, int &b){ int tmp = a; a = b; b = tmp; }};
方法2:
class Solution {public: vector<vector<int> > permute(vector<int> &num) { vector< vector< int> > res; if( num.size() <= 1){ res.push_back(num); return res; } vector< vector< int> > post; vector< int> cur = num; vector< int> tmp; for( int i = 0; i < num.size(); ++i){ cur = num; cur.erase(cur.begin()+i); post = permute(cur); for( int j = 0; j < post.size(); ++j){ tmp = post[j]; tmp.insert( tmp.begin(), num[i]); res.push_back(tmp); } } return res; }};
0 0
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- LeetCode: Permutations
- LeetCode: Permutations
- [Leetcode] Permutations
- [LeetCode] Permutations
- Leetcode: Permutations
- [Leetcode] Permutations
- [Leetcode] Permutations
- [LeetCode]Permutations
- LeetCode-Permutations
- [leetcode] permutations
- LeetCode - Permutations
- Leetcode: Permutations
- 【leetcode】Permutations
- 【LeetCode】Permutations
- PushbackReader
- 什么是hibernate中的二级缓存
- 比较MCU、DSP、ARM、SOC、FPGA
- Android Service示例
- 两个集合如何比较
- leetcode: Permutations
- 其实性格决定命运,细节决定成败,知识改变命运,学习成就未来,观念决定行动,思维决定出路,态度决定一切
- Orancle
- 根据sql脚本修改数据库表结构
- 第十章 数组和指针(复习题总结)总结 140521 22 23
- 容器 deque的例子
- PLSQL developer 连接不上64位Oracle 的解决方法
- MFC学习笔记——void CLi11View::OnDraw(CDC* /*pDC*/)
- Android Service的全面总结