Created
July 7, 2021 14:00
-
-
Save dongwooklee96/238ccf041d7565252618e1f20b78ebaa to your computer and use it in GitHub Desktop.
1.13.1
1.13.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 문제 1.13: 고유한 정수의 집합으로 배열이 주어지면, 가능한 모든 부분집합을 반환하자. | |
| 중복된 부분집합은 허용하지 않는다. | |
| 입력으로 [1, 2, 3]이 주어지면, 결과로 [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]] 을 반환하면 된다. | |
| ## 제한 사항 | |
| - 입력은 고유한 정수의 집합으로 이루어져있다. | |
| - 정수형 배열 | |
| - 반환값은 2차원 정수형 배열 | |
| - 입력 배열 요소는 중복값을 허용하지 않는다. | |
| ## 1. 아이디어 (NESTED FOR) | |
| - 리스트의 길이만큼 FOR | |
| - | |
| ## 2. 재귀를 이용한 방법 | |
| - | |
| ## 3. 반복문을 이용한 방법 | |
| ## 구현 | |
| """ | |
| from typing import List | |
| def subsets(nums: List[int]) -> List[List[int]]: | |
| res = [] | |
| nums_len = len(nums) | |
| nth_bit = 1 << nums_len | |
| for i in range(2 ** nums_len): | |
| bitmask = bin(i | nth_bit)[3:] | |
| res.append( | |
| [nums[j] for j in range(nums_len) if bitmask[j] == '1']) | |
| return res | |
| if __name__ == "__main__": | |
| nums = list(map(int, input().split())) | |
| print(subsets(nums)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment