Is there any efficiency boost for passing in nums and sub by reference in genSubsets() in the recursive solution? Just want to make sure I'm not missing anything.

Great idea! Very simple. Well, I am not quite comfortable with the recursive backtracking solution and fortunately I have seen your iterative solution.

This code seems to work well and looks very compact! Could you please explain a bit the idea behind it? How do you list all the subsets with i, j, where j is determined by the size of the results?

therefore, there are in fact multiple copies of strings of different states in the entire process of recursion, which is not efficient. in java, you can use a StringBuilder, whose content is stored in the heap, thus you should add and remove characters manually. in this way, there is only one copy of the string, it's just that its content keeps changing.

you can use long instead of int which allow you at most 64 elements. I don't think a computer could compute all subsets if a set has more than 64 elements.

I used copy library. But I think your solution with dictionary is more efficient! Thank you for the tips!

import copy
class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result=[[]]
n=len(nums)
nums.sort()
for i in range(n):
c = copy.deepcopy(result)
for k in c:
k.append(nums[i])
result.append(k)
return result