[Leetcode] (python): 078 subsets

Source: Internet
Author: User

Source of the topic

https://leetcode.com/problems/subsets/

Given a set of distinct integers, nums, return all possible subsets.

Note:

    • Elements in a subset must is in non-descending order.
    • The solution set must not contain duplicate subsets.

Test instructions Analysis

Input:

: Type Nums:list[int]

Output:

: Rtype:list[list[int]]

Conditions: Given a list with no duplicate elements, return all its subsets, noting that the elements in each subset are non-descending, and subsets cannot be duplicated.

Topic ideas

Use the DFS idea to add a number at a time. Because the list is sorted beforehand, the next time DFS should only traverse the element after the current position.

AC Code (PYTHON)

1 classsolution (object):2     defsubsets (Self, nums):3         """4 : Type Nums:list[int]5 : Rtype:list[list[int]]6         """7         defDfs (depth, start, valueList):8 res.append (valueList)9             ifdepth = =Len (nums):Ten                 return One              forIinchRange (Start, Len (nums)): ADFS (depth + 1, i + 1, valuelist+[Nums[i]]) -          - Nums.sort () theres = [] - DFS (0, 0, []) -         returnRes

[Leetcode] (python): 078 subsets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.