47. Permutations II
来源:互联网 发布:查询域名注册信息 编辑:程序博客网 时间:2024/05/29 05:53
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]]
类似46题,求一个数组的所有排列,但这里的数可能出现重复,排列要求不能重复。在46题的代码进行小小的修改,要保证排列不重复,则要保证开头的数不重复,然后递归求子数组的全排列。
代码:
class Solution {public: vector<vector<int>> permuteUnique(vector<int>& nums) { vector<vector<int>> res; permute_helper(nums, 0, res); return res; }private: void permute_helper(vector<int>& nums, int l, vector<vector<int>>&res) { if(l == nums.size()-1) { res.push_back(nums); return; } unordered_set<int> numSet; for(int i = l; i < nums.size(); ++i) { if(numSet.find(nums[i]) != numSet.end()) { continue; } numSet.insert(nums[i]); swap(nums[l], nums[i]); permute_helper(nums, l+1, res); swap(nums[l], nums[i]); } }};
阅读全文
0 0
- 46. Permutations && 47. Permutations II
- 46. Permutations, 47. Permutations II
- [LeetCode]47.Permutations II
- LeetCode 47.Permutations II
- LeetCode --- 47. Permutations II
- [Leetcode] 47. Permutations II
- [leetcode] 47.Permutations II
- 47.Permutations II
- 47. Permutations II
- LeetCode 47. Permutations II
- 47. Permutations II
- 47. Permutations II LeetCode
- 47. Permutations II
- 47. Permutations II
- 47. Permutations II
- 47. Permutations II
- Leetcode 47. Permutations II
- 47. Permutations II
- JZOJ 5419. 【NOIP2017提高A组集训10.24】筹备计划
- MATLAB教程(1) MATLAB 基础知识
- 数组的特点,数组的使用,链表的特点,set和list的区别,arrayList,linkedList,的常用方法,set,hashset,
- bzoj2013
- Data Stream Median
- 47. Permutations II
- 解决Unknown error: Unable to build: the file dx.jar was not loaded from the SDK folder!
- 监控中心方案设计
- python __slot__
- P
- Spark SQL 个人总结
- C++进阶—>互斥量:Event控制:多线程实现生产者-消费者例子
- new string("xiaolixi")的参数问题
- 线性表10个问答