46.全排列(Permutations)

原创 2018年04月16日 19:36:45

题目描述

给定一个包含不同数字的序列,返回其所有可能的全排列。

示例:

输入: [1,2,3]
输出:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

解题思路

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        // Arrays.sort(nums); // not necessary
        backtrack(list, new ArrayList<Integer>(), nums);
        return list;
    }

    private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums){
        if(tempList.size() == nums.length){
            list.add(new ArrayList<Integer>(tempList));
        } else{
            for(int i = 0; i < nums.length; i++){
                if(tempList.contains(nums[i])) continue; // element already exists, skip
                tempList.add(nums[i]);
                backtrack(list, tempList, nums);
                tempList.remove(tempList.size() - 1);
            }
        }
    }

46 - Permutations(全排列和递归)

Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] h...
  • hellochenlu
  • hellochenlu
  • 2016-03-03 10:37:44
  • 566

leetcode 46. Permutations-全排列|回溯|递归|非递归

原题链接:46. Permutations 【思路-Java、Python】递归实现 全排列是将一组数按一定顺序进行排列,如果这组数有n个,那么全排列数为n!个。现以{1, 2, 3, 4, 5}为例...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016-05-29 22:15:10
  • 4801

【Leetcode】【python】Permutations

题目大意求一组数的全排列解题思路回溯,想起来思路很简单,实际写的时候遇到了很多麻烦。代码正常思路class Solution(object): def permute(self, nums):...
  • qqxx6661
  • qqxx6661
  • 2017-10-03 11:15:45
  • 117

leetcode 46. Permutations(考全排列)

Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have...
  • a731604348
  • a731604348
  • 2017-04-10 00:26:23
  • 134

全排列,字典顺序问题 ( permutations/leetcode)

46. Permutations题目地址 https://leetcode.com/problems/permutations/注意是distinct numbers,所以相对简单, 当然写出好的代...
  • qq_26437925
  • qq_26437925
  • 2016-09-29 21:01:34
  • 427

leetcode 46,47. Permutations I/II 全排列问题 java

46.Permutations Total Accepted: 96713 Total Submissions: 271529 Difficulty: Medium Given a collect...
  • chenchaofuck1
  • chenchaofuck1
  • 2016-04-19 23:00:36
  • 2078

[leetcode-46]Permutations(java)

问题描述: Given a collection of numbers, return all possible permutations.For example, [1,2,3] have th...
  • zdavb
  • zdavb
  • 2015-08-02 17:23:41
  • 1175

LeetCode(46)Permutations

题目Given a collection of numbers, return all possible permutations.For example, [1,2,3] have the fol...
  • fly_yr
  • fly_yr
  • 2015-08-31 20:07:48
  • 2020

[LeetCode-Java]46. Permutations

Given a collection of distinct numbers, return all possible permutations.For example, [1,2,3] have ...
  • qq_32199531
  • qq_32199531
  • 2016-10-19 22:23:21
  • 511

[leetcode] 46. Permutations 解题报告

题目链接:https://leetcode.com/problems/permutations/ Given a collection of distinct numbers, return al...
  • qq508618087
  • qq508618087
  • 2016-01-14 16:14:55
  • 432
收藏助手
不良信息举报
您举报文章:46.全排列(Permutations)
举报原因:
原因补充:

(最多只允许输入30个字)