Why I got different output from OJ and my eclipse for the same input (3,7)?


  • 0
    C
    ret = []
    def combinationSum3(self, k, n):
        self.findTuple(k, n, [], 1)
        return self.ret
        
    def findTuple(self, k, n, tmp, start):
        if k<0 or n<0:
            return
        if k==0 and n==0:
            if sorted(tmp) not in self.ret:
                self.ret.append(sorted(tmp))
            return
        for cand in range(start, 10):
            if cand>n:
                break
            if cand not in tmp and cand<=n:
                temp = copy.deepcopy(tmp)
                temp.append(cand)
                self.findTuple(k-1, n-cand, temp, cand+1)

  • 0
    C

    by the way, OJ output [[1,5],[2,4],[1,2,4]]


  • 0

    Probably because you don't reset ret between test cases.


  • 0
    F

    I have encountered the same problem, and the same case : 3, 7,
    my code ran well in my circumstance, but the OJ print [1,5] [2,4] [1,2,4]
    I used class properties as well, but initialized it at each beginning!


  • 0
    F

    Sorry, Found the clue, I initialized the instance's variable, not the class variable.


Log in to reply
 

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