LeetCode 47: Permutations II Solution
Master LeetCode problem 47 (Permutations II), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
47. Permutations II
Problem Explanation
Explanation
To solve this problem, we can use backtracking to generate all unique permutations of the given array with duplicates. We will sort the input array to handle duplicates easily. During the backtracking process, we will keep track of used elements to avoid duplicates in the permutations.
-
Sort the input array to handle duplicates.
-
Initialize an empty list to store the result.
-
Implement a backtracking function that takes the current permutation, the array of used elements, and the input array as parameters.
-
In the backtracking function:
- If the current permutation's length is equal to the input array's length, add it to the result list.
- Iterate through the input array:
- If the current element is already used or equals the previous element and the previous element is not used, skip to avoid duplicates.
- Mark the current element as used, add it to the current permutation, and recursively call the backtracking function.
- Backtrack by marking the current element as unused and removing it from the current permutation.
-
Return the result list.
Time Complexity: O(N * N!), where N is the length of the input array. The time complexity is influenced by the number of unique permutations generated.
Space Complexity: O(N), where N is the length of the input array. The space complexity is due to the recursion stack and the result list.
Solution Code
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), new boolean[nums.length], nums);
return result;
}
private void backtrack(List<List<Integer>> result, List<Integer> tempList, boolean[] used, int[] nums) {
if (tempList.size() == nums.length) {
result.add(new ArrayList<>(tempList));
} else {
for (int i = 0; i < nums.length; i++) {
if (used[i] || (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])) {
continue;
}
used[i] = true;
tempList.add(nums[i]);
backtrack(result, tempList, used, nums);
used[i] = false;
tempList.remove(tempList.size() - 1);
}
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 47 (Permutations II)?
This page provides optimized solutions for LeetCode problem 47 (Permutations II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 47 (Permutations II)?
The time complexity for LeetCode 47 (Permutations II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 47 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 47 in Java, C++, or Python.