Why my backtracking solution doesn't have "add, recurse, remove" routine?


  • 0
    L
    def letterCombinations(self, digits):
        
        if not digits or len(digits) == 0:
            return []
        res = []
        self.DFS(digits, 0, "", res)
        return res
        
    def DFS(self, digits, cur, result, results):
        if len(result) == len(digits):
            results.append(result)
            return # backtracking
        else:
            for chr in self.digit2letters[digits[cur]]:
                self.DFS(digits, cur + 1, result + str(chr), results)
    

    This is my DFS solution using the backtracking thought. I just a little confused about the backtracking method in different problem. Why some of them need to first add one temptation and then recursion and then remove that temptation to go back to previously step. But why this problem don't need to do so?

    Anyone wants to explain this in detail? Thanks so much


Log in to reply
 

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