Combination Sum III - Python


  • 0
    M
    class Solution:
        
        arr = []
        temp = []
    
        def arrsame(self, site):
            if site > 0 and self.temp[site - 1] >= self.temp[site]:
                return False
            return True
    
        def arrprint(self, k, n):
            if sum(self.temp) == n:
                self.arr.append([self.temp[i] for i in range(k)])
    
        def calculate(self, k, n, now):
    
            if k == now:
                self.arrprint(k, n)
                return
    
            for i in range(1, 10):
                self.temp[now] = i
                if self.arrsame(now):
                    self.calculate(k, n, now + 1)
    
        def combinationSum3(self, k, n):
            self.arr = []
            self.temp = [0 for i in range(10)]
            self.calculate(k, n, 0)
            return self.arr
            
    

Log in to reply
 

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