leetcode---permutations-ii---dfs
来源:互联网 发布:返利 知乎 编辑:程序博客网 时间:2024/05/21 22:42
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2]have the following unique permutations:
[1,1,2],[1,2,1], and[2,1,1].
class Solution {public: bool ok(int i, int j, vector<int> &num) { for(int k=i; k<j; k++) { if(num[k] == num[j]) return false; } return true; } void dfs(int dep, int n, vector<vector<int>> &ans, vector<int> &num) { if(dep >= n) { ans.push_back(num); } for(int i=dep; i<n; i++) { if(ok(dep, i, num)) { swap(num[i], num[dep]); dfs(dep+1, n, ans, num); swap(num[i], num[dep]); } } } vector<vector<int> > permuteUnique(vector<int> &num) { vector<vector<int>> ans; int n = num.size(); if(n == 0) return ans; dfs(0, n, ans, num); return ans; }};
阅读全文
0 0
- leetcode---permutations-ii---dfs
- 【Leetcode】47. Permutations II 【DFS】
- [LeetCode]Permutations II(!!!!!DFS递归&&回溯)
- LeetCode-47-Permutations II(DFS/剪枝)-Medium
- [LeetCode]47. Permutations II && DFS及剪枝
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- 【DFS】Permutations II
- DFS Permutations II
- LeetCode 47. Permutations II(生成不同的组合-dfs)
- Leetcode: Permutations and Permutations II
- leetcode-permutations and permutations II
- Leetcode:Permutations与Permutations II
- LeetCode 46. Permutations DFS
- 【Leetcode】46. Permutations【DFS】
- leetcode---permutations---dfs
- 个人笔记
- 写给昨天的自己
- 字符串-KMP的模式匹配算法
- bootstrap插件bootstrapValidator常用验证规则总结
- 设计模式之工厂模式
- leetcode---permutations-ii---dfs
- Python3 输入和输出
- wordpress设置固定链接后文章出现404的解决办法 【ubuntu16.04】
- Python3 OS 文件/目录方法
- 排查中,windows部分浏览器下载文件的后缀名被更改
- VPN篇(5.6) 01. VPN 结构与需求 ❀ 飞塔 (Fortinet) 防火墙
- MySQL binlog 详解
- 智能指针
- 百练2811:熄灯问题(枚举、位运算)