【LeetCode】047. 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],
  [2,1,1]
]

  

题解:

Solution 1 (TLE)

class Solution {
public:
    void dfs(vector<int> nums, vector<vector<int>>& vv, vector<int>& v, vector<int>& visited, int level) {
        if(level >= nums.size()) {
            if(find(vv.begin(), vv.end(), v) == vv.end())
                vv.push_back(v);
            return;
        }
        for(int i=0; i<nums.size(); ++i) {
            if(visited[i] != 0) continue;
            v.push_back(nums[i]);
            visited[i] = 1;
            dfs(nums, vv, v, visited, level+1);
            v.pop_back();
            visited[i] = 0;
        }
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<vector<int>> vv;
        vector<int> v, visited(nums.size(),0);
        dfs(nums, vv, v, visited, 0);
        return vv;
    }
};

Solution 2 ()

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

Solution 3 ()

class Solution {
public:
    void dfs(vector<int> nums, set<vector<int>>& sv, int level) {
        if(level >= nums.size()) {
            sv.insert(nums);
            return;
        }
        for(int i=level; i<nums.size(); ++i) {
            if(i != level && nums[i] == nums[level]) continue;
            swap(nums[i], nums[level]);
            dfs(nums, sv, level+1);
            swap(nums[i], nums[level]);
        }
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        set<vector<int>> sv;
        vector<int> v, visited(nums.size(),0);
        sort(nums.begin(), nums.end());
        dfs(nums, sv, 0);
        return vector<vector<int>> (sv.begin(), sv.end());
    }
};

Solution 4 ()

class Solution {
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<vector<int>> ans;
        vector<int> aux(nums.begin(), nums.end());
        sort(aux.begin(), aux.end());
        do {
            ans.emplace_back(aux.begin(), aux.end());
        } while(next_permutation(aux.begin(), aux.end()));
        return ans;
    }
};
时间: 2024-11-10 14:08:41

【LeetCode】047. Permutations II的相关文章

【Leetcode】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

【LeetCode】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【Leetcode】Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

【LeetCode】Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

【LeetCode】Course Schedule II 解题报告

[题目] There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses an

【LeetCode】Jump Game II 解题报告

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of

【LeetCode】Unique Paths II 解题报告

[题目] Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the