[LeetCode] 78. Subsets - 파이썬(Python)

2022. 5. 12. 14:23·Solving Algorithm Problem/LeetCode

문제 링크 : https://leetcode.com/problems/subsets/

 

Subsets - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

유형 : 백트래킹(BackTracking)

문제 설명

중복되지 않는 숫자들이 담긴 배열 nums가 주어질 때, nums의 요소들로 만들 수 있는 모든 하위 집합을 return 하는 문제이다. 모든 하위 집합을 중복없이 임의의 순서로 return 하면 된다.

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]]

제한사항

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • nums의 모든 숫자들은 unique하다.

문제 풀이

성공 코드

from typing import List

class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        self.nums = nums
        self.subsets = []
        
        self.bt(0, [])
        
        return self.subsets
    
    def bt(self, index: int, crnt_set: List[int]):
        if index == len(self.nums):
            self.subsets.append(crnt_set.copy())
            return

        num = self.nums[index]
        self.bt(index+1, crnt_set)

        crnt_set.append(num)
        self.bt(index+1, crnt_set)
        crnt_set.pop()

시간 복잡도 : O(n*2^n)

공간 복잡도 : O(n) 

저작자표시 (새창열림)
'Solving Algorithm Problem/LeetCode' 카테고리의 다른 글
  • [LeetCode] 39. Combination Sum - 파이썬(Python)
  • [LeetCode] 77. Combinations - 파이썬(Python)
  • [LeetCode] 46. Permutations - 파이썬(Python)
  • [LeetCode] 17. Letter Combinations of a Phone Number - 파이썬(Python)
김행만
김행만
이모저모 다 적기
  • 김행만
    hyeinisfree
    김행만
  • 전체
    오늘
    어제
    • 분류 전체보기 (41)
      • AWS (0)
      • Network (6)
      • CICD (1)
      • Spring (0)
      • Ruby on Rails (0)
      • Java (12)
      • Python (1)
      • Computer Science (6)
        • Algorithm (2)
        • Data Structure (3)
        • Database (0)
        • Design Pattern (1)
      • Solving Algorithm Problem (12)
        • LeetCode (12)
        • Programmers (0)
      • 자격증 (1)
      • Tip (1)
      • Etc (1)
        • 도서, 강의 (1)
        • Talk (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • GitHub
  • 공지사항

  • 인기 글

  • 태그

    Network
    cisco packet tracer
    java
    CISCO
    java 필수 문법
    Packet Tracer
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
김행만
[LeetCode] 78. Subsets - 파이썬(Python)
상단으로

티스토리툴바