[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],

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]
]

[LeetCode]–31. Next Permutation

做过这个题之后,肯定启发很多,所以我很快就写出来了。

public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if (nums.length == 0)
            return res;
        Arrays.sort(nums);
        joinlist(nums, res);
        findPermute(nums, res);
        return res;
    }

    private void findPermute(int[] nums, List<List<Integer>> res) {
        int i = nums.length - 1;
        while (i > 0 && nums[i] <= nums[i - 1])
            i--;
        if (i == 0)
            return;
        int second = Integer.MAX_VALUE, secondIndex = Integer.MAX_VALUE;
        for (int j = nums.length - 1; j > i - 1; j--) 
            if (nums[j] > nums[i - 1] && nums[j] < second){
                second = nums[j];
                secondIndex = j;
            }
        int temp = nums[i - 1];
        nums[i - 1] = nums[secondIndex];
        nums[secondIndex] = temp;
        Arrays.sort(nums, i, nums.length);
        joinlist(nums, res);
        findPermute(nums, res);
    }

    private void joinlist(int[] nums, List<List<Integer>> res) {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < nums.length; i++)
            list.add(nums[i]);
        res.add(list);
    }

找了一个另外的答案,也AC了,但是效率没有我写的高,不过看起来代码简洁一些。可以参考参考。简单理解一下就是找出规律,总共有n!这么多个序列,可以分为(n-1)!这么多个组,每个组n个,这样来通过for循环找到所有答案。

public List<List<Integer>> permute(int[] nums) {
         ArrayList<List<Integer>> rst = new ArrayList<List<Integer>>();
         if (nums == null) {
             return rst; 
         }

         if (nums.length == 0) {
            rst.add(new ArrayList<Integer>());
            return rst;
         }

         ArrayList<Integer> list = new ArrayList<Integer>();
         helper(rst, list, nums);
         return rst;
    }

    public void helper(ArrayList<List<Integer>> rst, ArrayList<Integer> list, int[] nums){
        if(list.size() == nums.length) {
            rst.add(new ArrayList<Integer>(list));
            return;
        }

        for(int i = 0; i < nums.length; i++){
            if(list.contains(nums[i])){
                continue;
            }
            list.add(nums[i]);
            helper(rst, list, nums);
            list.remove(list.size() - 1);
        }

    }
目录
相关文章
|
人工智能
LeetCode 47. Permutations II
给定一组可能有重复元素不同的整数,返回所有可能的排列(不能包含重复)。
68 0
LeetCode 47. Permutations II
LeetCode 46. Permutations
给定一组不同的整数,返回所有可能的排列。
49 0
|
算法
[LeetCode]--47. 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],
1046 0
LeetCode - 46. Permutations
46. Permutations  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数组,求这个数组的全排列.
892 0
LeetCode - 47. Permutations II
47. Permutations II  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数组(元素可能重复),求这个数组的全排列.
814 0
[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 o...
892 0
[LeetCode] Permutations II
Well, have you solved the nextPermutation problem? If so and you have handled the cases of duplicates at that problem, your code can be used in this problem.
692 0