題目:Given a set of distinct integers,nums, return all possible subsets.
Note:
For example,
Ifnums=[1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]解題思路:搜了一下網上的解題思路,有一個感覺很巧妙。大致的思路是這樣的,一個數組的子集個數是這個數組的長度的n次方(2^n),例如a[2]={1,2},子集個數就是2^2=4,二進制表示為00,01,10,11,分別表示空集、{1}、{2}、{12},具體的實現代碼如下:
public class Solution { public List> subsetsWithDup(int[] nums) { List
> result=new ArrayList
>(); //先對數組進行排序 Arrays.sort(nums); int n=nums.length; //1<
list=new ArrayList (); for(int j=0;j