classSolution {
public:bool sumOfNumberAndReverse(int num) {
for (int x =0; x <= num; ++x) {
int y =0, t = x;
while (t) { y = y *10+ t %10; t /=10; }
if (x + y == num) return true;
}
return false;
}
};
1
2
3
4
5
6
7
8
9
10
classSolution {
publicbooleansumOfNumberAndReverse(int num) {
for (int x = 0; x <= num; ++x) {
int y = 0, t = x;
while (t > 0) { y = y * 10 + t % 10; t /= 10; }
if (x + y == num) returntrue;
}
returnfalse;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funsumOfNumberAndReverse(num: Int): Boolean {
for (x in0..num) {
var y = 0var t = x
while (t > 0) {
y = y * 10 + t % 10 t /=10 }
if (x + y == num) returntrue }
returnfalse }
}
1
2
3
4
5
6
7
classSolution:
defsumOfNumberAndReverse(self, num: int) -> bool:
for x in range(num+1):
y = int(str(x)[::-1])
if x + y == num:
returnTruereturnFalse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
impl Solution {
pubfnsum_of_number_and_reverse(num: i32) -> bool {
for x in0..=num {
letmut y =0;
letmut t = x;
while t >0 {
y = y *10+ t %10;
t /=10;
}
if x + y == num { returntrue; }
}
false }
}
1
2
3
4
5
6
7
8
9
10
11
functionsumOfNumberAndReverse(num: number):boolean {
for (letx=0; x<=num; ++x) {
lety=0, t=x;
while (t>0) {
y=y*10+t%10;
t= Math.floor(t/10);
}
if (x+y===num) returntrue;
}
returnfalse;
}