[LeetCode] Sum of Number and Its Reverse

2443. Sum of Number and Its Reverse

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
long long reverse(int n) {
int res = 0;
while(n) {
res = res * 10 + n % 10;
n /= 10;
}
return res;
}
public:
bool sumOfNumberAndReverse(int num) {
for(int i = 0; i <= num; i++) {
if(i + reverse(i) == num) return true;
}

return false;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/16/PS/LeetCode/sum-of-number-and-its-reverse/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.