[leedcode 46] 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 {
    //全排列:构造一个递归函数,函数的参数一个代表开始排列的索引,一个代表最终排列的索引
    //每一位与开始位进行交换,再递归start+1到end,注意结果的保存
    List<Integer> seq;
    List<List<Integer>> res;
    public List<List<Integer>> permute(int[] nums) {
        seq=new ArrayList<Integer>();
        res=new ArrayList<List<Integer>>();
        findpermute(nums,0,nums.length-1);
        return res;

    }
    public void findpermute(int []nums,int start,int end){
        if(start>end){
             res.add(new ArrayList<Integer>(seq));//注意要重新new一个
            return ;
        }

        for(int i=start;i<=end;i++){
            swap(nums,start,i);
            seq.add(nums[start]);
            findpermute(nums,start+1,end);
            seq.remove(seq.size()-1);//注意删除
            swap(nums,start,i);
        }
    }
    public void swap(int[] nums,int i,int j){
        int temp=nums[i];
        nums[i]=nums[j];
        nums[j]=temp;
    }
}
时间: 2025-01-01 10:31:10

[leedcode 46] Permutations的相关文章

&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

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

[LeedCode OJ]#46 Permutations

[ 声明:版权所有,转载请标明出处,请勿用于商业用途.  联系信箱:[email protected]] 题目链接:https://leetcode.com/problems/permutations/ 题意: 给定一个数组,要求返回其所有全排列的情况 思路: 对于一个特定排列我们有一个求下一个全排列的函数,那就是next_permutation,运用这个函数这道题迎刃而解 class Solution { public: vector<vector<int> > permute(

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