LeetCode
来源:互联网 发布:linux history文件 编辑:程序博客网 时间:2024/06/06 19:52
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]]
求一个数列的全排列。
直接dfs即可。时间复杂度O(n^2),空间复杂度O(n)
class Solution {public: vector<vector<int>> permute(vector<int>& nums) { vector<vector<int> > ans; if (nums.empty()) return ans; sort(nums.begin(), nums.end()); vector<int> cur; vector<bool> vis(nums.size(), false); dfs(ans, nums, cur, vis); return ans; } void dfs(vector<vector<int> >& ans, vector<int>& nums, vector<int>& cur, vector<bool>& vis) { if (cur.size() == nums.size()) { ans.push_back(cur); return; } for (int i = 0; i < nums.size(); ++i) { if (vis[i]) continue; vis[i] = true; cur.push_back(nums[i]); dfs(ans, nums, cur, vis); cur.pop_back(); vis[i] = false; } return; }};
阅读全文
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- 线程的互斥与同步通信
- Bug库____org.hibernate.HibernateException: No Session found for current thread
- 百度地图开发者平台API项目应用
- Android 使用Zxing实现二维码,横切竖屏预览扭曲变开的解决方法
- TCP异常处理(accept返回前连接中止)与SO_LINGER选项
- LeetCode
- mysql去除外网权限访问限制
- supervisor使用详解
- MySql的存储过程练习
- Android Proguard-rules.pro中常用的混淆配置
- Akka(16): 持久化模式:PersistentFSM-可以自动修复的状态机器
- 在Android中使用SyncAdapter同步数据全攻略
- HTML5和纯CSS3动画源码
- Jupyter Notebook各种使用方法记录