Question: https://oj.leetcode.com/problems/subsets-ii/
Question Name: Subsets II
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class Solution: # @param num, a list of integer # @return a list of lists of integer def subsetsWithDup(self, S): ''' A problem to test the bit operation. ''' S.sort() # Make sure the result is non-descending order. used = set() # The used subset in result limit = 1 << len(S) current = 0 # In "current", the i(th) rightmost bit == 1 # means S[i] is chosen. result = [] while current < limit: # Construct the list for "current" temp = [] for index in xrange(len(S)): if (1 << index) & current != 0: temp.append(S[index]) tempTuple = tuple(temp) if not tempTuple in used: result.append(temp) used.add(tempTuple) current += 1 return result |