Skip to content

Instantly share code, notes, and snippets.

@Manjago
Created August 2, 2023 19:53
Show Gist options
  • Save Manjago/cc6e18d6b4aa63c5aad16ede1ce54f0d to your computer and use it in GitHub Desktop.
Save Manjago/cc6e18d6b4aa63c5aad16ede1ce54f0d to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
// 2023008-02 46. Permutations https://leetcode.com/problems/permutations/ by ChatGPT
public class LeetCode00046_1 {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
permuteHelper(nums, new boolean[nums.length], new ArrayList<>(), result);
return result;
}
private void permuteHelper(int[] nums, boolean[] used, List<Integer> current, List<List<Integer>> result) {
if (current.size() == nums.length) {
result.add(new ArrayList<>(current));
return;
}
for (int i = 0; i < nums.length; i++) {
if (!used[i]) {
current.add(nums[i]);
used[i] = true;
permuteHelper(nums, used, current, result);
used[i] = false;
current.remove(current.size() - 1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment