Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 1 ms, faster than 97.30% of Java online submissions for Permutations.
- // Memory Usage: 37.4 MB, less than 95.74% of Java online submissions for Permutations.
- class Solution {
- public List<List<Integer>> permute(int[] nums) {
- ArrayList<List<Integer>> result = new ArrayList<>();
- permute(nums, nums.length, new ArrayList<>(), result, new boolean[nums.length]);
- return result;
- }
- void permute(int[] nums, int n, List<Integer> current, List<List<Integer>> result, boolean[] chosen) {
- if (current.size() == n) {
- result.add(new ArrayList<>(current));
- return;
- }
- for (int i = 0; i < n; i++) {
- if (chosen[i]) {
- continue;
- }
- chosen[i] = true;
- current.add(nums[i]);
- permute(nums, n, current, result, chosen);
- current.remove(current.size() - 1);
- chosen[i] = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement