Problem: Permutation

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

Example 1:

Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

Example 2:

Input: nums = [0,1] Output: [[0,1],[1,0]] Example 3:

Input: nums = [1] Output: [[1]]

//Constraints:

1 <= nums.length <= 6 -10 <= nums[i] <= 10 All the integers of nums are unique.

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

    void recursion(int i, int[] nums) {
        if(i == nums.length) {
            List<Integer> list = new ArrayList<>();
            for(int j : nums) list.add(j);
            res.add(new ArrayList<>(list));
            return;
        }

        for(int x = i; x < nums.length; x++){
            swap(i, x, nums);
            recursion(i+1, nums);
            swap(i, x, nums);
        }
    }
    void swap(int i, int x, int[] nums){
        int temp = nums[i];

        nums[i] = nums[x];
        nums[x] = temp;
        return;
    }
}

111.png