Permutations LeetCode
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]]
import java.util.*;
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>(); // to store all permutations
boolean[] used = new boolean[nums.length]; // track used elements
List<Integer> path = new ArrayList<>(); // current permutation path
backtrack(nums, path, used, result);
return result;
}
private void backtrack(int[] nums, List<Integer> path, boolean[] used, List<List<Integer>> result) {
if (path.size() == nums.length) {
result.add(new ArrayList<>(path)); // important: create new list and add
return;
}
for (int i = 0; i < nums.length; i++) {
if (used[i]) continue; // skip if already used
// Choose
path.add(nums[i]);
used[i] = true;
// Explore
backtrack(nums, path, used, result);
// Unchoose (Backtrack)
path.remove(path.size() - 1);
used[i] = false;
}
}
}
Comments
Post a Comment