Problem solution · Python

Sum of Number and Its Reverse

Sum of Number and Its Reverse: a Python solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Depth-first search
Source
Kamyu LeetCode Solutions
Length
65 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Depth-first search

For Sum of Number and Its Reverse, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 65 lines of Python from the credited upstream file sum-of-number-and-its-reverse.py.
  • The implementation keeps its working state in language-native values and containers.
  • No explicit loop blocks detected.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeSum of Number and Its Reverse · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(2^(log10(n)/2)) = O(n^(1/(2*log2(10))))# Space: O(log10(n)/2) # backtrackingclass Solution(object):    def sumOfNumberAndReverse(self, num):        """        :type num: int        :rtype: bool        """        def backtracking(num, chosen):            if num == 0:                return True            if chosen == 1:                return False            if num <= 18:                return (num%2 == 0) or (num == 11 and chosen == 0)            if chosen == 2:                return False            for x in (num%10, 10+num%10):                if not (1 <= x <= 18):                    continue                base = 11                if chosen:                    base = chosen                else:                    while x*((base-1)*10+1) <= num:                        base = (base-1)*10+1                if num-x*base >= 0 and backtracking((num-x*base)//10, base//100+1):                    return True            return False         return backtracking(num, 0)  # Time:  O(nlogn)# Space: O(1)# brute forceclass Solution2(object):    def sumOfNumberAndReverse(self, num):        """        :type num: int        :rtype: bool        """        def reverse(n):            result = 0            while n:                result = result*10 + n%10                n //= 10                        return result         return any(x+reverse(x) == num for x in xrange(num//2, num+1))  # Time:  O(nlogn)# Space: O(logn)# brute forceclass Solution3(object):    def sumOfNumberAndReverse(self, num):        """        :type num: int        :rtype: bool        """        return any(x+int(str(x)[::-1]) == num for x in xrange(num//2, num+1)) 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗