Microsoft Coding Interview Question | Leetcode 78 | Subsets

preview_player
Показать описание
In this video, we introduce how to solve the "Subsets" question which is used by big tech companies like Google, Facebook, Amazon in coding interviews. We also cover how to behave during a coding interview, e.g. communication, testing, coding style, etc.

Please subscribe to this channel if you like this video. I will keep updating this channel with videos covering different topics in interviews from big tech companies.

Рекомендации по теме
Комментарии
Автор

# Python solution slightly better using iteration
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
unique_subsets = []
len_nums = len(nums)
number_of_subsets = 2 ** len_nums
i = 0
while i < number_of_subsets:
bin_num = bin(i)[2:][-1::-1]
subset = []
for j, x in enumerate(bin_num):
if x == '1':
subset.append(nums[j])

i = i + 1
return unique_subsets

angelsancheese
Автор

# Python version of your code
class Solution:
def recursive(self, nums, i, unique_list, subset):
if len(nums) == i:
temp = list(subset)
unique_list.append(temp)
return

self.recursive(nums, i+1, unique_list, subset)
subset.append(nums[i])
self.recursive(nums, i+1, unique_list, subset)
subset.pop()


def subsets(self, nums: List[int]) -> List[List[int]]:
unique_list = []
subset = []
self.recursive(nums, 0, unique_list, subset)
return unique_list

angelsancheese
visit shbcf.ru