Last updated
Was this helpful?
Last updated
Was this helpful?
Given a list of numbers that may has duplicate numbers, return all possible subsets
Each element in a subset must be in non-descending order.
The ordering between two subsets is free.
The solution set must not contain duplicate subsets.
Example
If S =[1,2,2]
, a solution is:
Can you do it in both recursively and iteratively?
方法跟前面一样,去重跟 一样
非递归还是ctci,去重比较麻烦。要记住长度,只加到后面的子集。
例子:[1, 2, 2]
当集合加到{[], [1], [2], [1, 2]} 的时候,用1的方法继续加的话会产生重复。{[], [1], [2], [1, 2], [2], [1, 2], [2, 2], [1, 2, 2]} 为了去掉中间连个,我们只能把新的数字加到上一回生曾的那些集合里。这里是[2], [1, 2]