LeetCode46 47 full array

Leetcode 46 returns all possible combinations
Insert picture description here
for a set of unduplicated numbers to find the full arrangement of an array. For the subscript index of the array (0~arr.length-1), we need to consider placing each number in the array in The condition of the subscript.

public class Permutations {
    
    
    public static List<List<Integer>> permute(int[] nums) {
    
    
        List<List<Integer>> result = new LinkedList<>();
        helper(nums, 0, result);
        return result;
    }

    private static void helper(int[] nums, int index, List<List<Integer>> result) {
    
    
        if (index == nums.length) {
    
    
            List<Integer> list = new LinkedList<>();
            Arrays.stream(nums).forEach(list::add);
            result.add(list);
            return;
        }
        for (int i = index; i < nums.length; i++) {
    
    
            // 将其他位置的数字放置到数组的index下标处
            swap(nums, i, index);
            // 递归交换数字到其它下标处
            helper(nums, index + 1, result);
            // 将数组恢复为原始的状态,进行下一次交换
            swap(nums, i, index);
        }
    }

    private static void swap(int[] nums, int i, int j) {
    
    
        int temp = nums[j];
        nums[j] = nums[i];
        nums[i] = temp;
    }
}

Leetcode 47
Insert picture description here
has repeated elements, we need to determine whether the element has been placed in this position before exchanging element elements. If it has been placed, it will not be placed anymore. The following isPlaced() function plays such a role .

class Solution {
    
    
    public static List<List<Integer>> permuteUnique(int[] nums) {
    
    
        List<List<Integer>> result = new LinkedList<>();
        helper(nums, 0, result);
        return result;
    }

    private static void helper(int[] nums, int index, List<List<Integer>> result) {
    
    
        if (index == nums.length) {
    
    
            List<Integer> list = new LinkedList<>();
            Arrays.stream(nums).forEach(list::add);
            result.add(list);
            return;
        }
        for (int i = index; i < nums.length; i++) {
    
    
            if (isPlaced(nums,index,i)) {
    
    
                continue;
            }
            swap(nums, i, index);
            // 其实这里不用添加,index等于size的时候nums中就是最终结果
            helper(nums, index + 1, result);
            swap(nums, i, index);
        }
    }


    private static boolean isPlaced(int[] nums, int start, int end) {
    
    
    // 如果在end下标之前存在一个和end相同的元素,那么在start位置是已经防止过了的,我们就不再放置
        for (int i = start; i < end; i++) {
    
    
            if (nums[i] == nums[end])//当前元素已经是重复元素
                return true;
        }
        return false;
    }

    private static void swap(int[] nums, int i, int j) {
    
    
        int temp = nums[j];
        nums[j] = nums[i];
        nums[i] = temp;
    }

    public static void main(String[] args) {
    
    
        System.out.println(permuteUnique(new int[]{
    
    1, 0, 0, 0, 9}));
    }
}

Guess you like

Origin blog.csdn.net/liu_12345_liu/article/details/104105669