zoukankan      html  css  js  c++  java
  • 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]
    ]

    AC code:

    class Solution {
    public:
        vector<vector<int>> permuteUnique(vector<int>& nums) {
            vector<vector<int>> v;
            sort(nums.begin(), nums.end());
            recursion(nums, v, 0, nums.size()-1);
            return v;
        }
    
        void recursion(vector<int> nums, vector<vector<int>>& v, int i, int j) {
            if (i == j) {
                v.push_back(nums);
                return;
            }
            for (int k = i; k <= j; ++k) {
                if (i != k && nums[i] == nums[k])
                    continue;
                swap(nums[i], nums[k]);
                recursion(nums, v, i+1, j);
            }
        }
    };
    

    Runtime: 28 ms, faster than 46.26% of C++ online submissions for Permutations II.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    hashlib模块
    logging模块
    Python的富比较方法
    格式化符号说明
    __str__与__repr__区别
    2014-07-18&nbsp;10:25

    2014-07-17&nbsp;17:04
    2014-07-17&nbsp;16:44
    2014-07-16&nbsp;15:54
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9806601.html
Copyright © 2011-2022 走看看