class Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort() ans,stack,x,n=[],,0,len(nums) while True: if x<n: stack+=[(x,nums[x])] ans+=[zip(*stack)] x+=1 elif stack: x=stack.pop()+1 else: return ans
Is there any alternative without using tuple? The way I can think of is either to maintain two stacks, or still only maintaining one stack of the indices but then we need to get the corresponding nums[x] when appending to ans. Both of them seem to be even slower. Any suggestion is welcome!