Permutations II
Thoughts
Code
class Solution {
private void helper(int[] nums, boolean[] visited, List<Integer> path, List<List<Integer>> res) {
if (path.size() == nums.length) {
res.add(new ArrayList<Integer>(path));
}
for (int i = 0; i < nums.length; i++) {
if (visited[i] || i > 0 && nums[i] == nums[i - 1] && !visited[i - 1]) {
continue;
}
visited[i] = true;
path.add(nums[i]);
helper(nums, visited, path, res);
visited[i] = false;
path.remove(path.size() - 1);
}
}
public List<List<Integer>> permuteUnique(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
helper(nums, new boolean[nums.length], new ArrayList<>(), res);
return res;
}
}Analysis
Last updated