Simpler Iterative Python Solution (without using a stack)

  • 0
    class Solution(object):
        def combinationSum3(self, k, n):
            :type k: int
            :type n: int
            :rtype: List[List[int]]
            combinations = []
            masks = [ 1 << x for x in range(9)]
            for rank in range(2 ** 9):
                nums = [x+1 for x in range(9) if rank & masks[x] != 0]
                if len(nums) == k and sum(nums) == n:
            return combinations

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.