LeetCode 46. Permutations
来源:互联网 发布:人工智能ai技术展会 编辑:程序博客网 时间:2024/05/16 09:49
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]
, and [3,2,1]
.
Classical backtrack.
In order to make the output in sequence, sort the inputs first.
void permute(vector<int>& nums, int depth, vector<vector<int>>& res, vector<int>& path, vector<int>& used) { if(depth == nums.size()) res.push_back(path); // depth variable can be saved, if here checks path.size() == nums.size() for(int i = 0; i < nums.size(); ++i) { if(used[i] == false) { used[i] = true; // use a flag to mark that the value has been used. path.push_back(nums[i]); permute(nums, depth + 1, res, path, used); used[i] = false; path.pop_back(); } } } vector<vector<int>> permute(vector<int>& nums) { if(nums.size() == 0) return {}; sort(nums.begin(), nums.end()); vector<vector<int>> res; vector<int> path; vector<int> used(nums.size(), false); permute(nums, 0, res, path, used); return res; }
0 0
- [LeetCode]46.Permutations
- LeetCode 46.Permutations
- 46. Permutations Leetcode Python
- LeetCode --- 46. Permutations
- [Leetcode] 46. Permutations
- [leetcode] 46.Permutations
- LeetCode 46. Permutations DFS
- 46. Permutations LeetCode
- Leetcode 46. Permutations
- leetcode 46. Permutations
- LeetCode *** 46. Permutations
- LeetCode 46. Permutations
- leetcode 46. Permutations
- LeetCode - 46. Permutations
- 【leetcode】46. Permutations
- leetcode 46. Permutations
- [LeetCode] 46. Permutations
- leetcode 46. Permutations
- LeetCode 279. Perfect Squares(完美平方)
- hadoop 多个mapreduce在java中串起来执行
- 深入理解JAVA内存模型——基础
- Andoird粗圆环的半径求取。
- 求二叉树节点数 -- 采用递归和非递归方法
- LeetCode 46. Permutations
- Xcode7.2 / OS X EI Capitan 10.11 提交不了 AppStore
- Android控件架构
- Android中自定义样式与View的构造函数中的第三个参数defStyle的意义
- View的测量
- 字节序 大端,小端
- View的绘制
- 参加第四个项目(第6970小时时)
- Mybatis学习笔记02