-
Notifications
You must be signed in to change notification settings - Fork 0
/
9_palindrome_number.cpp
52 lines (48 loc) · 1.6 KB
/
9_palindrome_number.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <string>
using namespace std;
// Solution 1: Convert to string and compare characters from the two ends
class Solution_1 {
public:
bool isPalindrome(int x) {
// Check if the number is negative
if (x < 0) {
return false;
}
// Convert the number to a string
string s = to_string(x);
// Iterate over the characters in the string
for (int i = 0; i < s.size() / 2; i++) {
// Check if the characters at the two ends of the string are different
if (s[i] != s[s.size() - i - 1]) {
return false;
}
}
// Return true if the string is a palindrome
return true;
}
};
// Solution 2: Only get the half of the number and compare
class Solution_2 {
public:
bool isPalindrome(int x) {
// Special cases:
// As discussed above, all negative numbers are not palindrome.
// Also if the last digit of the number is 0, in order to be a palindrome,
// the first digit of the number also needs to be 0.
// Only 0 satisfy this property.
if (x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int revertedNumber = 0;
while (x > revertedNumber) {
revertedNumber = revertedNumber * 10 + x % 10;
x /= 10;
}
// When the length is an odd number, we can get rid of the middle digit by
// revertedNumber/10 For example when the input is 12321, at the end of the
// while loop we get x = 12, revertedNumber = 123, since the middle digit
// doesn't matter in palindrome(it will always equal to itself), we can
// simply get rid of it.
return x == revertedNumber || x == revertedNumber / 10;
}
};