Problem Description
Given a non-negative integer num, determine if it can be expressed as the sum of a non-negative integer and its reverse.
Key Insights
- The equation is x + reverse(x) = num.
- Since both x and reverse(x) are non-negative, x must be between 0 and num.
- Reversing a number is straightforward, with care for leading zeros in the reversed number.
- A brute-force search from 0 to num is acceptable because num is at most 10^5.
Space and Time Complexity
Time Complexity: O(num) in the worst-case, where num ≤ 10^5. Space Complexity: O(1) since only a constant amount of extra space is used.
Solution
We iterate over all possible values of x from 0 to num. For each x, we compute its reverse and check if the sum (x + reverse(x)) equals num. The helper function to reverse a number involves converting the number to a string, reversing the string, and then converting it back to an integer. The brute-force search works given the constraint, and no additional data structures are needed.