iT邦幫忙

2022 iThome 鐵人賽

0
自我挑戰組

LeetCode Top 100 Liked系列 第 40

[Day 40] Subsets (Medium)

  • 分享至 

  • xImage
  •  

78. Subsets

Solution 1: Bitwise

class Solution(object):
    def subsets(self, nums):
        N = len(nums)
        allPossibleCombinCnt = 1 << N # 2**N
        allCombin = []
        for bitMask in range(allPossibleCombinCnt): 
            currCombin = []
            for btI in range(N):
                if bitMask & (1 << btI):
                    currCombin.append(nums[btI])
            allCombin.append(currCombin)
        return allCombin

Time Complexity: O(2^N)
Space Complexity: O(1)

Solution 2: DFS

Time Complexity: O(2^N)
Space Complexity: O(2^N)

Reference

https://www.cnblogs.com/grandyang/p/4309345.html


上一篇
[Day 39] Number of Islands (Medium)
下一篇
[Day 41] Next Permutation (Medium)
系列文
LeetCode Top 100 Liked77
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言