LeetCode: Permutations [045]

【题目】

Given a collection of numbers, return all possible permutations.

For example,

[1,2,3] have the following permutations:

[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2],
and [3,2,1].

【题意】

给定一个数组,生成所有的全排列

【思路】

递归,类DFS

【代码】

class Solution {
public:
    void dfs(vector<vector<int> >&result, vector<int>combination, vector<int>candidates, int index2remove){
        //combination——当前已生成的组合
        //candidates——候选数字集合
        //index2remove——添加到combination的下一个数字在candidates中的索引位
        combination.push_back(candidates[index2remove]);
        candidates.erase(candidates.begin()+index2remove);
        if(candidates.empty()){
            result.push_back(combination);
        }
        else{
            for(int i=0; i<candidates.size(); i++){
                dfs(result, combination, candidates, i);
            }
        }

    }
    vector<vector<int> > permute(vector<int> &num) {
        vector<vector<int> >result;
        int size=num.size();
        if(size==0)return result;

        vector<int> combination;
        for(int i=0; i<num.size(); i++){
            dfs(result, combination, num, i);
        }
        return result;
    }
};

LeetCode: Permutations [045],布布扣,bubuko.com

时间: 2024-10-22 01:05:28

LeetCode: Permutations [045]的相关文章

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:Permutations 排列

戳我去解题 Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 解题分析:首先进行排序,保证结果保持字典序 class Solution { public: vector<vector<int>

LeetCode: Permutations 解题报告

Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. SOLUTION 1: 经典的递归回溯题目,一次ACCEPT. 请也参考上一个题目LeetCode: Combination

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

Well, have you solved the nextPermutation problem? If so, your code can be used in this problem. The idea is fairly simple: sort nums in ascending order, add it to res; generate the next permutation of nums using nextPermutation(), and add it to res;

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——Permutations

Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 用Python递归求全排列,回顾回顾 class Permutations: def perm(self, num, li,

[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 就

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排序,然