546.Add a Number to Its Reverse
Easy
Given a non-negative integer
n, return n plus its digit-reversal. For example, 123 + 321 = 444. Leading zeros created by reversing are dropped, so 120 reverses to 21.
Reversing digits is useful on its own, so it belongs in a helper functionHelper FunctionA small function that does one focused job, such as reverseNumber(n), so the main function can call it by name.. With the helper in place, the main function reads exactly like the problem statement. The two solutions reverse in different ways — through text, or with digit arithmeticDigit ArithmeticUsing % 10 to read the last digit and / 10 to remove it, so a number can be processed digit by digit. — but the main function is identical in both.
Example 1:
Input: n = 123
Output: 444
Example 2:
Input: n = 120
Output: 141
+ 5 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ n ≤ 100000000
n =
123