题目

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

解答

C语言的实现以前总结过,请见 http://www.cnblogs.com/YuNanlong/p/6171459.html

这道题做的我很气,自己写交换vector元素的过程,只击败了30+%的提交,用了swap()函数击败了70+%的。。。

下面是AC的代码:

class Solution {
public:
    vector<vector<int>> ans;
    vector<int> res;
    void permute(vector<int> nums){
        int length = nums.size();
        if(length == 1){
            res.push_back(nums[0]);
            ans.push_back(vector<int>(res.begin(), res.end()));
            res.pop_back();
            return ;
        }
        else{
            int last;
            for(int i = 0; i < length; i++){
                if(i == 0){
                    last = nums[i];
                }
                else if(last == nums[i]){
                    continue;
                }
                last = nums[i];
                swap(nums[0], nums[i]);
                res.push_back(last);
                permute(vector<int>(nums.begin() + 1, nums.end()));
                res.pop_back();
            }
        }
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        permute(vector<int>(nums.begin(), nums.end()));
        return ans;
    }
};

107

LeetCode OJ 47. Permutations II的更多相关文章

  1. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  2. 【一天一道LeetCode】#47. Permutations II

    一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...

  3. 【LeetCode】47. Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  4. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  5. LeetCode 【47. Permutations II】

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

  6. LeetCode OJ:Permutations II(排列II)

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

  7. [LeetCode] 47. Permutations II 全排列之二

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

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

    Permutations II  Given a collection of numbers that might contain duplicates, return all possible un ...

  9. leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列

    字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...

随机推荐

  1. Go语言 数据类型,流程控制

    Go语言 数据类型,流程控制 人生苦短,Let's Go ! package main // 必须要有一个main包 import "fmt" func main() { fmt. ...

  2. conda命令

  3. eMTC/NB/LTE拨号

    挂起-恢复流程挂起恢复流程是eMTC/NB-IoT等蜂窝物联网技术才引进的,LTE并不具备这样的流程.这种机制的引入主要针对物联网海量连接,不活跃小数据包的特点,适时的挂起流程可以减少网络的资源开销, ...

  4. prvReadAsyncOperation

    prvReadAsyncOperation privilege is the Read privilege for System Job Entity (Role Customizationtab). ...

  5. html代码段

    添加icon<link rel="shortcut icon" href="img/100du.ico"/>

  6. spi and sensor

    http://blog.csdn.net/DroidPhone/article/details/23367051 https://www.kernel.org/doc/html/v4.14/drive ...

  7. time random sys os 模块

    时间模块 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串: (1)时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日 ...

  8. Elasticsearch 2.4 安装

    Ubuntu 18.04.1 Part I. Elasticsearch 1. install JDK Note: >1.8 reference: <Linux下安装Tomcat> ...

  9. css实战第三天小结

    1.谈一谈对层级的理解: 如果对两个并列的子元素都设置了相对于同一个父元素(如果没有设置父元素那么默认相对于浏览器而言)进行了定位(相对定位),则这两个都具有相同的层级(默认为0),他们的trbl也默 ...

  10. Java - 19 Java 异常处理

    Java 异常处理 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用 ...