leetcode--Permutations
来源:互联网 发布:给矩阵怎么计算行列式 编辑:程序博客网 时间:2024/06/07 15:20
1.问题
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]
.
回溯法求解,保证每个元素不同,搜索时不能用二分搜索,因为每个元素无序。
3.实现
class Solution {public: vector<vector<int>> permute(vector<int>& nums) { vector<int> one; solve(nums,one,0); return allPermute; }private: vector<vector<int>> allPermute; void solve(vector<int> &nums,vector<int> &one,int k) { int n = nums.size(); for(int i=0;i<n;++i) { one.push_back(nums[i]); if(!isExist(one,nums[i])) { if(k==n-1) allPermute.push_back(one); else solve(nums,one,k+1); } one.pop_back(); } } bool isExist(vector<int> &one,int x) { for(int i=0;i<one.size()-1;++i) { if(one[i]==x) return true; } return false; }};
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
- iOS沙盒目录结构解析
- 查看LINUX进程内存占用情况
- Win版4S降级工具odysseusOTA4教程百分之百成功
- IOS 隐藏 app 桌面 图标
- 四大组件之BroadcastReceive
- leetcode--Permutations
- 物联网行业的关注点
- PagerAdapter的使用方法
- 从特征描述子到深度学习:计算机视觉的20年历程 From feature descriptors to deep learning: 20 years of computer vision
- 搭建NFS共享文件夹
- Qt 操作sqlite总结
- html入门
- 首先CSS优先级
- Centos免密码登录别人系统(单人维护模式)