46. Permutations (Recursion, DP)

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> > permute(vector<int> &num) {
        result.clear();
        dfs(num,0);
        return result;

    }
    void dfs(vector<int> num, int depth)
    {
        if(depth == num.size()-1)
        {
            result.push_back(num);
            return;
        }
        dfs(num,depth+1);
        int temp = num[depth];
        for(int i = depth+1;i< num.size(); i++)
        {
            num[depth] = num[i];
            num[i] = temp;
            dfs(num,depth+1);
            num[i] = num[depth];
            num[depth] = temp;
        }
    }

private:
    vector<vector<int> >  result;
};
时间: 2024-12-26 20:20:10

46. Permutations (Recursion, DP)的相关文章

[Lintcode]15. Permutations/[Leetcode]46. Permutations

15. Permutations/46. Permutations 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers, return all possible permutations. Example Example 1: Input: [1] Output: [ [1]] Example 2: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3]

刷题46. Permutations

一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解: 刷题31. Next Permutation 我考虑可以用dp做,写了一个上午,理论我就不说了,自己看代码: #include<iostream> #include<vector> #include<unordered_map> using namespace std;

&lt;LeetCode OJ&gt; 46. Permutations

46. Permutations My Submissions Question Total Accepted: 81495 Total Submissions: 239854 Difficulty: Medium Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,

leetCode 46. Permutations 回溯问题 | Medium

46. Permutations(全排列问题--回溯问题经典) Given a collection of distinct 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],   [3,2,1] ] 题目大意:求一个序列的全排列. 思路:做排列

DFS解法的两道题 Leetcode 46 Permutations &amp; Leetcode 78 Subset

Leetcode 78 Subset Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1

46. Permutations 排列数

46. Permutations 题目 Given a collection of distinct 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], [3,2,1] ] 解析 class Solution_46 { public: void help(int

LeetCode 46. Permutations

原题 Given a collection of distinct 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], [3,2,1] ] 解题思路 递归:递归的方法,创建一个visit判断此值是否已经添加过,每一层不断地循环,加入没有被访问的元素,直到最后结果的长

46. Permutations

Given a collection of distinct 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], [3,2,1] ] 题解 该题是求所有可能的排列组合,是一道典型的dfs类型的题 代码(python实现) import copy class Solu

Uva 11077 Find the Permutations [置换群 DP]

题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个排列是一样的 排列就是置换,分解循环,然后显然每个循环变成升序需要$len-1$次交换 然后有$t$个循环的置换需要$n-t$次交换 $DP$就行了$f[i][j]$表示前$i$个数有$j$个循环 其实可以发现就是第一类$stirling$数 注意:以后一定要测一遍极限会爆$long\ long$