publicArrayList<ArrayList<Integer>>subsets(int[] nums) {ArrayList<ArrayList<Integer>> res =newArrayList<>();if (nums ==null||nums.length==0) {return res; }// sort to add elements in non-descending order这只是题目要求Arrays.sort(nums);ArrayList<Integer> tmp =newArrayList<>();helper(nums,0, tmp, res);return res; }privatevoidhelper(int[] nums,int start,ArrayList<Integer> tmp,ArrayList<ArrayList<Integer>> res) {// everytime in add tmp to resres.add(newArrayList<Integer>(tmp));// loop to add other element into the set// loop from start, not like permutation, // we don't need to pick every numberfor (int i = start; i <nums.length; i++) {// 递归结束条件是loop完所有数字一遍tmp.add(nums[i]);helper(nums, i +1, tmp, res);//下一层递归加下一个数字tmp.remove(tmp.size() -1); } }