Leetcode 46 Permutations
来源:互联网 发布:知乎wv 编辑:程序博客网 时间:2024/05/02 17:51
Given a collection of distinct numbers, return all possible permutations.
For example,[1,2,3]
have the following permutations:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]找出全排列。
直接用next_permutation实现
class Solution {public: vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> result; sort(nums.begin(),nums.end()); do { result.push_back(nums); }while(next_permutation(nums.begin(),nums.end())); return result; }};DFS实现
class Solution {public: void dfs(vector<vector<int>> &result,vector<int> nums,vector<int> now) { if(nums.size() == 0) { result.push_back(now); return ; } for(int i=0;i<nums.size();i++) { now.push_back(nums[i]); vector<int> temp=nums; temp.erase(temp.begin()+i); dfs(result,temp,now); now.pop_back(); } } vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> result; vector<int> now; dfs(result,nums,now); return result; }};
0 0
- LeetCode 46 Permutations + LeetCode 47 Permutations II
- LeetCode 46: Permutations
- leetcode 46 Permutations
- LeetCode 46 Permutations II
- LeetCode(46) Permutations
- [leetcode 46] Permutations
- Leetcode NO.46 Permutations
- leetcode || 46、Permutations
- leetcode-46 Permutations
- leetcode 46 : Permutations
- Leetcode #46 Permutations
- LeetCode(46) Permutations
- leetcode 46: Permutations
- [leetcode-46]Permutations(java)
- LeetCode(46)Permutations
- [Leetcode]#46 Permutations
- leetcode 46:Permutations
- 【leetcode】【46】Permutations
- mfc编译、使用sqlite
- Java Enum
- Openstack 基本模块单机部署练习
- sqlite数据类型 datetime处理
- 全面解读java虚拟机(面试考点大全)
- Leetcode 46 Permutations
- python3.3~mysql模块:'int' does not support the buffer interface
- Sprite kit中 anchorPoint和zPosition 的用法
- 394.leetcode Decode String(medium)[栈 字符串]
- iOS中监测来电方案
- C++ Tricks(一)—— 判断字符串 string 对象的所有字符都相等
- 缺少动态连接库.so--cannot open shared object file: No such file or directory
- 用js+css模拟九宫格精灵版本二
- 打造原生侧滑菜单