lc-78


//Given an integer array nums of unique elements, return all possible subsets (t
//he power set). 
//
// The solution set must not contain duplicate subsets. Return the solution in a
//ny order. 
//
// 
// Example 1: 
//
// 
//Input: nums = [1,2,3]
//Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
// 
//
// Example 2: 
//
// 
//Input: nums = [0]
//Output: [[],[0]]
// 
//
// 
// Constraints: 
//
// 
// 1 <= nums.length <= 10 
// -10 <= nums[i] <= 10 
// All the numbers of nums are unique. 
// 
// Related Topics Array Backtracking Bit Manipulation 
// 👍 11287 👎 167


//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        if (nums == null) {
            return null;
        }

        Set<List<Integer>> resultSet = new HashSet<>();

        Arrays.sort(nums);
        List<Integer> curList = new ArrayList<>();
        calSubset(resultSet, curList, nums, 0);
        return new ArrayList<>(resultSet);

    }

    void calSubset(Set<List<Integer>> resultSet, List<Integer> curList, int[] nums, int index) {
        if (index == nums.length) {
            List<Integer> copyList = new ArrayList<>(curList);
            resultSet.add(copyList);
            return;
        }

        curList.add(nums[index]);
        calSubset(resultSet, curList, nums, index + 1);
        curList.remove(curList.size() - 1);
        calSubset(resultSet, curList, nums, index + 1);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

文章作者: 倪春恩
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 倪春恩 !