Sum of Number and Its Reverse — LeetCode 2443 Python Solution
- Problem
- #2443
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Given a non-negative integer num, return true if num can be expressed as the sum of any non-negative integer and its reverse, or false otherwise.
Example
- Input
- num = 443
- Output
- true
- Explanation
- 172 + 271 = 443 so we return true.
Python solution
class Solution:
def sumOfNumberAndReverse(self, num: int) -> bool:
return any(k + int(str(k)[::-1]) == num for k in range(num + 1))Complexity
| Measure | Complexity |
|---|---|
| Time | O(n \times \log n), where n is the size of num |
| Space | O(1) auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 2443. Sum of Number and Its Reverse is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2443. Sum of Number and Its Reverse?
- LeetCode 2443. Sum of Number and Its Reverse is rated Medium on LeetCode.
- What is the time complexity of LeetCode 2443. Sum of Number and Its Reverse?
- The Python solution on this page runs in O(n \times \log n), where n is the size of num.
- What is the space complexity of LeetCode 2443. Sum of Number and Its Reverse?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2443. Sum of Number and Its Reverse cover?
- LeetCode 2443. Sum of Number and Its Reverse is tagged Math and Enumeration on LeetCode.