Backtracking
1 | class Solution { |
Remarks:
- Prunning is important:
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) continue;We skip the same items in one layer.!used[i - 1]means that we can use the duplicated number in different layers (because it was used when we callbacktrakingto process the next layer.)used[i - 1]only changes tofalsewhen a combination is fulfilled andbacktrackingreturns. - TC: $O(n!)$, SC: $O(n!\times n)$