g0001_0100.s0047_permutations_ii.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java17 Show documentation
Show all versions of leetcode-in-java17 Show documentation
Java Solution for LeetCode algorithm problems, continually updating
package g0001_0100.s0047_permutations_ii;
// #Medium #Array #Backtracking #Algorithm_II_Day_10_Recursion_Backtracking
// #2022_06_16_Time_2_ms_(93.29%)_Space_48_MB_(59.58%)
import java.util.ArrayList;
import java.util.List;
public class Solution {
private List> ans;
public List> permuteUnique(int[] nums) {
ans = new ArrayList<>();
permute(nums, 0);
return ans;
}
private void permute(int[] nums, int p) {
if (p >= nums.length - 1) {
List t = new ArrayList<>(nums.length);
for (int n : nums) {
t.add(n);
}
ans.add(t);
return;
}
permute(nums, p + 1);
boolean[] used = new boolean[30];
for (int i = p + 1; i < nums.length; i++) {
if (nums[i] != nums[p] && !used[10 + nums[i]]) {
used[10 + nums[i]] = true;
swap(nums, p, i);
permute(nums, p + 1);
swap(nums, p, i);
}
}
}
private void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}