[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].

Show Tags

Backtracking


这个就是输入一个数组,可能有重复,输出全部的排列。

这题调用stl 就是如下了:

 1 #include <vector>
 2 #include <iterator>
 3 #include <algorithm>
 4 #include <iostream>
 5 using namespace std;
 6
 7 class Solution {
 8 public:
 9     vector<vector<int> > permuteUnique(vector<int> &num) {
10         int n = num.size();
11         vector<vector<int> >ret;
12         if(n<1) return ret;
13         if(n<2) {ret.push_back(num);    return ret; }
14         sort(num.begin(),num.end());
15         ret.push_back(num);
16         while(next_permutation(num.begin(),num.end())){
17             ret.push_back(num);
18         }
19         return ret;
20     }
21 };
22
23 int main()
24 {
25     vector<int> num = {1,1,2};
26     Solution sol;
27     vector<vector<int> > ret = sol.permuteUnique(num);
28     for(int i=0;i<ret.size();i++){
29         copy(ret[i].begin(),ret[i].end(),ostream_iterator<int>(cout," "));
30         cout<<endl;
31     }
32     return 0;
33 }

如果不调用嘛,就是自己写一个next_permutation,在Permutations 写过好多个版本了,回顾下stl 的实现逻辑吧:

  1. 输入数组a[],从右向左遍历,寻找相邻的两个数,使得 left<mid,没找到?就是没有,返回false了。
  2. 再次从右往左遍历,寻找right >left, 这次的right 不需要一定与left 相连,因为有1 这部,所以有保底的取值(mid)
  3. 交换left 与right。
  4. 逆向mid 与其右边。
  5. 结束。
时间: 2024-09-29 02:01:51

[LeetCode] Permutations II 排列的相关文章

LeetCode: Permutations II [046]

[题目] 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]. [题意] 给定一个候选数集合,候选集中可能存在重复数,返回所有的排列 [思路] 思路和Permutat

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: 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]. SOLUTION 1: 还是经典的递归模板.需要处理的情况是:我们先把Num排序,然

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]. 原题链接:https://oj.leetcode.com/problems/permutations-ii/ 题

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]. 难度:80+15(怎样跳过重复的case来节省时间).我一开始的想法很简单,就是在permutation这道题的

[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刷题笔记】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]. 题解:跟Permutation差不多,只是这次会有重复的元素,如下图所示,如果只用DFS的话就会产生重复的排列: 上

leetcode 刷题之路 77 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 的升级版,依旧是全排列问题,但是序列中可能会出现重复数字. 思路:采用字典序的非递归方

Permutations II leetcode java

题目: 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]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一