[LeetCode]047-Permutations II
来源:互联网 发布:淘宝代销怎么取消关联 编辑:程序博客网 时间:2024/05/18 02:54
题目:
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].
Solution:
直接运用nextPermutation的思想。nextPermutation,按照字典序排序的,字符串的下一个字典序全排列。
class Solution {public: void nextPermutation(vector<int>& nums) { int N = nums.size(); int i = N - 1; while(i > 0 && (nums[i-1] >= nums[i])) i--; int j = i; while( j < N && i != 0 && nums[j] >nums[i-1]) j++; if(i == 0) reverse(nums.begin(),nums.end()); else { swap(nums[i-1],nums[j-1]); reverse(nums.begin()+i,nums.end()); } } vector<vector<int>> permuteUnique(vector<int>& nums) { vector<vector<int>> ret; vector<int> temp = nums; ret.push_back(temp); nextPermutation(temp); while(temp != nums) { ret.push_back(temp); nextPermutation(temp); } return ret; }};
0 0
- LeetCode 047 Permutations II
- LeetCode 047 Permutations II
- LeetCode 047 Permutations II
- leetcode 047 Permutations II
- [LeetCode]047-Permutations II
- LeetCode 047 Permutations II
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- Leetcode: Permutations and Permutations II
- leetcode-permutations and permutations II
- Leetcode:Permutations与Permutations II
- leetcode 047 —— Permutations II
- leetcode解题方案--047--Permutations II
- LeetCode: Permutations II
- LeetCode Permutations II
- LeetCode: Permutations II
- alsa 和 oss 声音系统[转]
- linux mp3乱码
- Spark-Sql之DataFrame实战详解
- dojo CheckBox问题解析
- iOS的开发中的错误解决方案
- [LeetCode]047-Permutations II
- 屏幕适配
- python的正则表达式re模板
- 经典的基于图着色模型的寄存器分配
- Dubbo源码分析(六):Dubbo内核实现之动态编译
- debug时按F8/F6不起作用
- Linux/Unix inode、vnode、dentry、file、进程表、文件表(下)
- sqlite数据库部署到服务器上的问题
- iOS vs Android 系统架构