link: 9. Palindrome Number

Solution 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
bool isPalindrome(int x) {
int a = 0, b = x;
while(b > 0) {
a = a * 10 + b % 10;
b /= 10;
}
if(a == x)
return true;
else
return false;
}
};

Solution 2 (Best Runtime Distribution):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0)
{
return false;
}
else if (x < 10)
{
return true;
}

int o = x;
long long result = 0;
while (x)
{
result = 10 * result + x % 10;
x = x / 10;
}
return result > INT_MAX ? false : result == o;
}
};

static const auto _____ = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return nullptr;
}();

评论