LeetCode 046 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].

分析:

参考网址:http://www.cnblogs.com/panda_lin/archive/2013/11/12/permutations.html

可以使用STL的next_permutation函数。

不使用的话,要递归生成全排列。

① 生成[2, 3]的全排列[2, 3]和[3, 2],然后把1加上去生成[1, 2, 3]和[1, 3, 2]。

② 交换1和2的位置,生成[1, 3]的全排列[1, 3]和[3, 1],然后把2加上去生成[2, 1, 3]和[2, 3, 1]。

③在第二步的基础上交换2和3的位置,生成[2, 1]的全排列[2, 1]和[1, 2],然后把3加上去生成[3, 2, 1]和[3, 1, 2]。

① [1, 2, 3]  [1, 3, 2]

② [2, 1, 3]  [2, 3, 1]

③ [3, 2, 1]  [3, 1, 2]

代码如下:

class Solution {
public:
    vector<vector<int> > permute(vector<int> &num) {

        vector<vector<int>> result;
        sort(num.begin(), num.end());

        //STL next_permutation
        do{
            result.push_back(num);
        }while(next_permutation(num.begin(), num.end()));

        return result;

    }
};
class Solution {
public:
    void internalPermute(vector<int> &num, int index, vector<int> &perm, vector<vector<int> > &result) {
        int size = num.size();

        if (size == index) {
            result.push_back(perm);
        }
        else {
            for (int i = index; i < size; ++i) {
                swap(num[index], num[i]);
                perm.push_back(num[index]);
                internalPermute(num, index + 1, perm, result);
                perm.pop_back();
                swap(num[index], num[i]);
            }
        }
    }

    vector<vector<int> > permute(vector<int> &num) {
        vector<vector<int> > result;
        vector<int> perm;

        internalPermute(num, 0, perm, result);

        return result;
    }
};
时间: 2024-10-13 19:20:06

LeetCode 046 Permutations的相关文章

【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]. public class Solution { public ArrayList<ArrayList<Integer>> permute

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].题解:依旧使用的是DFS的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,

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

【LeetCode】046. 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] ] 题解: 之前解过Next Permutation,故这个题可以重复调用nextPermutation函数产生全排列 Solu

[LeetCode][JavaScript]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]. https://leetcode.com/problems/permutations

LeetCode 46 Permutations(全排列问题)

题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组能够产生的所有全排列 采用递归算法,传入参数 List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used 其中list保存最终结果 tempList保存其中一个全排列 nums为最初的

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] ] 题目链接:https://leetcode.com/problems/permutations/ 题目大意:求全排列 题目分析:求

LeetCode (18) Permutations I &amp; II (排列一、二)

不存在重复的情况:题目描述 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]. 本题要求输入一数组,输出数组的所有排列方式.题目中给定的为int类型的数组,有些题目中给定的为字符类型的,两种思路是一

leetcode 46 Permutations Python 实现(回溯算法)

Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3]Output:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]] 回溯算法自己的一个思路如下:需要保留数字使用情况,my_nums增加和删除元素等操作 1 class Solution: 2 def permute(self, nums: Li