LeetCode OJ 之 Permutations II(排列 - 二)
来源:互联网 发布:linux spi dma 编辑:程序博客网 时间:2024/05/17 04:08
题目:
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]
.
思路:
DFS。参考:http://blog.csdn.net/u012243115/article/details/42965749#t4 。
代码1:
class Solution {public: vector<vector<int> > permuteUnique(vector<int> &num) { int len = num.size(); vector<vector<int> > result; sort(num.begin(),num.end()); result.push_back(num); if(len == 0) return result; while(next_permutation(num.begin(), num.end())) { result.push_back(num); } return result; }};
代码2:
class Solution {public: vector<vector<int> > permuteUnique(vector<int> &num) { vector<vector<int> > result; DFS(num,0,result); return result; } void DFS(vector<int> &num , int begin , vector<vector<int> > &result) { if(begin == num.size()) { result.push_back(num); return; } for(int i = begin ; i < num.size() ; i++) { if(!isSame(num , begin , i)) { swap(num[begin],num[i]); DFS(num,begin+1,result); swap(num[i],num[begin]); } } } bool isSame(vector<int> num , int begin , int end) { for(int i = begin ; i < end ; i++) { if(num[end] == num[i]) return true; } return false; }};
0 0
- LeetCode OJ 之 Permutations II(排列 - 二)
- LeetCode OJ 之 Permutations (排列)
- LeetCode (18) Permutations I & II (排列一、二)
- LeetCode OJ:Permutations II
- LeetCode OJ Permutations II
- LeetCode 47. Permutations II(排列)
- LeetCode:Permutations, Permutations II(求全排列)
- leetcode之Permutations II
- 【Leetcode】之Permutations II
- [LeetCode]—Permutations II 求全排列(有重复值)
- leetcode-47. Permutations II(重复元素全排列)
- LeetCode OJ-46.Permutations(全排列问题)
- LeetCode进阶之路(Permutations II)
- leetcode 47. Permutations II-排列|回溯算法
- LeetCode-----46.Permutations&&47.Permutations II (全排列----回溯法)
- <LeetCode OJ> 47/46 Permutations (II / I)
- Permutations II 排列II
- LeetCode OJ 之 Path Sum II (路径和 - 二)
- 昨天湿鞋了。。
- 人工智能作业——八数码问题 BFS+A* C# WindowsFormApplication
- LINUX目录结构
- Android利用Jsoup解析html 开发网站客户端小记
- 关于Android中Service与Activity的信息交互
- LeetCode OJ 之 Permutations II(排列 - 二)
- My linux install fedora
- Jsoup开发网站客户端第二篇,图片轮播,ScrollView兼容ListView
- 016 break和Contiune语句
- hadoop-java:表与表的左连接
- 亿级用户下的新浪微博平台架构
- 开发任意网站Android客户端教程
- 59-通讯录案例
- ubuntu文件/文件夹都带锁标记