Question: https://oj.leetcode.com/problems/combinations/
Question Name: Combinations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Solution: # @return a list of lists of integers def combine(self, n, k): pool = range(1, n+1) # The candidates to choose limit = 1 << len(pool) current = 0 # In current, bit[i] == 1 means S[i] is chosen. result = [] while current < limit: # Construct the list for "current" temp = [] for index in xrange(len(pool)): if (1 << index) & current != 0: temp.append(pool[index]) if len(temp) > k: # List is too long break else: if len(temp) == k: # List is qualified for result result.append(temp) # Else, it is too short current += 1 return result |