Permutations II
来源:互联网 发布:网络言情小说家排名 编辑:程序博客网 时间:2024/05/20 10:22
Permutations II
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], [2,1,1]]解析:
分析的有点复杂,每次对后面的数排序,所有后面的数每次跟第一个数交换。
代码:
class Solution {public: vector<vector<int>> permuteUnique(vector<int>& nums) { sort(nums.begin(),nums.end()); vector<int>path; vector<vector<int>>ans; int begin=0; dfs(nums,path,ans,begin); return ans; } void dfs(vector<int> nums, vector<int>&path,vector<vector<int>>&ans,int begin) { sort(nums.begin()+begin,nums.end()); int len=nums.size(); if (begin==len) { ans.push_back(path); return ; } int pre=INT_MIN; for (int i=begin; i<len; i++) { if (pre==nums[i]) continue; pre=nums[i]; if (i!=begin&&nums[i]==nums[begin]) continue; int temp=nums[begin]; nums[begin]=nums[i]; nums[i]=temp; path.push_back(nums[begin]); dfs(nums,path,ans,begin+1); temp=nums[begin]; nums[begin]=nums[i]; nums[i]=temp; path.pop_back(); } return ; } };
0 0
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- Permutations and Permutations II
- Permutations && Permutations ii
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Android Handler消息机制
- Java中Concurrent包中同步互斥纪录
- 【Front-end】如何使用html框架
- BZOJ 1260: [CQOI2007]涂色paint
- hint:this error often happens when you are running the packager(local dev server)
- Permutations II
- Java内部类
- 1.字符串匹配算法
- python学习心得(4)— 并发小米应用商城APK爬取
- java桌面程序打包教程
- 《算法导论》课后题--6--第四章(1)
- ECNU-3260
- Redis和Memcache对比及选择
- 在html中调用js实例