Permutation II
Given a collection of numbers, nums
, that might contain duplicates, return all possible unique permutations in any order.
有重复数字: [1,2,1]
sort, 在前一个相同数字被选之前,不能选当前数字,否则会出现 1pi, 1 == 1, 1pi的情况
Subset II
Given an integer array nums
that may contain duplicates, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
sort,核心情况:假设第i个和第i+1个相同,当回溯从i返回,接下来循环到第i+1个时,不应该选中i+1,因为所有包含i+1的可能情况,在i的回溯中都已经遇到过了,跳过i+1
Combination Sum II
Given a collection of candidate numbers (candidates
) and a target number (target
), find all unique combinations in candidates
where the candidate numbers sum to target
.
Each number in candidates
may only be used once in the combination.
Note: The solution set must not contain duplicate combinations.
和combination sub的情况是一样的,核心也是当退出i的回溯时,排除掉i+1的回溯(如果i+1和i num相同,那么可以假设所有可能情况都已经考虑过了)
Palindrom String
简单回溯
总结:找到所有可能性问题,都可以考虑回溯算法