Java中的Leetcode排列问题!清单如何处理这个问题?

时间:2020-06-18 14:23:17

标签: java list data-structures

public List<List<Integer>> permute(int[] nums) {
    List<List<Integer>> result = new ArrayList<>();
    Queue<List<Integer>> permutations = new LinkedList<>();
    permutations.add(new ArrayList<>());

    for(int currentNumber : nums){
        int n = permutations.size();
        for(int i=0; i<n; i++){
            List<Integer> oldPermutation = permutations.poll();
            for(int j=0; j<=oldPermutation.size(); j++){
        List<Integer> newPermutation = new ArrayList<Integer>(oldPermutation);
                newPermutation.add(j, currentNumber);
                if(newPermutation.size()==nums.length){
                    result.add(newPermutation);
                } else{
                    permutations.add(newPermutation);
                }
            }
        }
    }

    return result;
}

在粗体行中 List newPermutation = new ArrayList(oldPermutation);

我们为什么要传递oldPermutation以及()在Java列表中的作用是什么?

Queue<List<Integer>> permutations = new LinkedList<>()这行是做什么的。它是常规清单吗 例如[1,2,3]或链接列表?

谢谢!

1 个答案:

答案 0 :(得分:2)

  • 您正在使用LinkedList定义队列。它们是可以在Java中与这些数据结构一起使用的特定方法/操作。

  • 对于第二个问题,输出所需的数据结构是列表列表。使用oldPermutation成功生成了该元素,然后使用newPermutation添加了当前元素。最后,一旦它达到所需的大小(基于result语句),我们便在if中进行推送。

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<List<Integer>> permutations = new LinkedList<>();
        permutations.add(new ArrayList<>());

        for (int currentNumber : nums) {
            int n = permutations.size();

            for (int i = 0; i < n; i++) {
                List<Integer> oldPermutation = permutations.poll();

                for (int j = 0; j <= oldPermutation.size(); j++) {
                    List<Integer> newPermutation = new ArrayList<>(oldPermutation);
                    newPermutation.add(j, currentNumber);

                    if (newPermutation.size() == nums.length)
                        result.add(newPermutation);

                    else
                        permutations.add(newPermutation);
                }
            }
        }

        return result;
    }
}

这是LeetCode的解决方案,您正在尝试解决:

class Solution {
    public void backtrack(int n,
                          ArrayList<Integer> nums,
                          List<List<Integer>> output,
                          int first) {
        // if all integers are used up
        if (first == n)
            output.add(new ArrayList<Integer>(nums));

        for (int i = first; i < n; i++) {
            // place i-th integer first
            // in the current permutation
            Collections.swap(nums, first, i);
            // use next integers to complete the permutations
            backtrack(n, nums, output, first + 1);
            // backtrack
            Collections.swap(nums, first, i);
        }
    }

    public List<List<Integer>> permute(int[] nums) {
        // init output list
        List<List<Integer>> output = new LinkedList();

        // convert nums into list since the output is a list of lists
        ArrayList<Integer> nums_lst = new ArrayList<Integer>();

        for (int num : nums)
            nums_lst.add(num);

        int n = nums.length;
        backtrack(n, nums_lst, output, 0);
        return output;
    }
}

您可以使用其他类型的数据结构,具体取决于您要如何解决此问题,例如使用HashSet:

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        backtrack(res, new ArrayList<>(), new HashSet<>(), nums);
        return res;
    }

    private void backtrack(List<List<Integer>> res, List<Integer> tempList, Set<Integer> tempSet, int[] nums) {
        if (tempSet.size() == nums.length) {
            res.add(new ArrayList<>(new ArrayList<>(tempList)));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (tempSet.contains(nums[i]))
                continue;
            tempSet.add(nums[i]);
            tempList.add(nums[i]);
            backtrack(res, tempList, tempSet, nums);
            tempSet.remove(tempList.get(tempSet.size() - 1));
            tempList.remove(tempList.size() - 1);
        }
    }
}

参考文献