LeetCode-Subsets
Posted IncredibleThings
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-Subsets相关的知识,希望对你有一定的参考价值。
Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example, If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
public class Solution { public List<List<Integer>> subsets(int[] nums) { if(nums==null){ return null; } List<List<Integer>> resList=new ArrayList<List<Integer>>(); List<Integer> item=new ArrayList<Integer>(); Arrays.sort(nums); backTracking(nums, 0, item, resList); resList.add(new ArrayList<Integer>()); return resList; } public void backTracking(int[] nums, int start, List<Integer> item, List<List<Integer>> resList){ for(int i=start; i<nums.length; i++){ item.add(nums[i]); resList.add(new ArrayList<Integer>(item)); backTracking(nums, i+1, item, resList); item.remove(item.size()-1); } } }
以上是关于LeetCode-Subsets的主要内容,如果未能解决你的问题,请参考以下文章