Subsets 题解
Last updated
Was this helpful?
Last updated
Was this helpful?
题目来源:
> Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
解题思路:
注意输出的每个子集要有序。
跟一样。
每个元素都有0/1两种状态,全部排列一下即可。例如1,2,3,4一共有2^4=16种子集,第15种(2^0+2^1+2^2+2^3)为1-4都取, 第7种(1*(2^0)+1*(2^1)+1*(2^2)+0*(2^3))
为[1,2,3]. . 注意得先将S排序(当然也可以先加到result中,最后再来排序), 不然结果中的子集顺序不是升序的。