19.2.7 [LeetCode 47] Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

Example:

Input: [1,1,2]
Output:
[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]

 1 class Solution {
 2 public:
 3     vector<vector<int>> permuteUnique(vector<int>& nums) {
 4         set<vector<int>>ans;
 5         ans.insert(vector<int>(1, nums[0]));
 6         for (int i = 1; i < nums.size(); i++) {
 7             set<vector<int>>tmp = ans;
 8             ans.clear();
 9             for (auto j = tmp.begin(); j != tmp.end(); j++) {
10                 vector<int>now = *j;
11                 for (int k = 0; k <= now.size(); k++) {
12                     now.insert(now.begin() + k, nums[i]);
13                     ans.insert(now);
14                     now.erase(now.begin() + k);
15                 }
16             }
17         }
18         return vector<vector<int>>(ans.begin(),ans.end());
19     }
20 };

单纯只是把上一道改了一下

原文地址:https://www.cnblogs.com/yalphait/p/10354993.html

时间: 2024-11-05 21:16:20

19.2.7 [LeetCode 47] Permutations II的相关文章

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

16. Permutations II/47. Permutations II 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers with duplicate number in it. Find all unique permutations. Example Example 1: Input: [1,1] Output: [ [1,1]] Example 2: Input: [1,2,2] O

leetCode 47.Permutations II (排列组合II) 解题思路和方法

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], and [2,1,1]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

LeetCode 47 Permutations II(全排列)

题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description 给出数组,数组中的元素可能有重复,求出所有的全排列 使用递归算法: 传递参数 List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used 其中list保存最终结果 tempList保存其中一个全排列组合 nums保存初始的数组 used随着计

LeetCode: 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], and [2,1,1].题解:依旧使用的是DFS的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,

[LeetCode][JavaScript]Permutations II

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], and [2,1,1]. https://leetcode.com/problems/permutations

LeetCode 047 Permutations II

题目要求: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], and [2,1,1]. 代码如下: class Solution { public: vector

【LeetCode】Permutations II

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], and [2,1,1]. 首先分析一下与Permutations有何差异. 记当前位置为start,当前排列数

[C++]LeetCode: 120 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], and [2,1,1]. 思路:这道题和Permutations的区别就是,输入的数字数组中包含重复的元素.如果我们对重复的元素不

[leetcode] 47. 全排列 II

47. 全排列 II 比上一个题多了个重复性 与46. 全排列完全一样的代码... class Solution { // 当没有下一个排列时return false public boolean nextPermutation(int[] nums) { if (nums.length == 1) { return false; } int p = -1; for (int i = nums.length - 2; i >= 0; i--) { if (nums[i] < nums[i +