forked from kaidul/LeetCode_problems_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome_Number.cpp
More file actions
33 lines (32 loc) · 826 Bytes
/
Copy pathPalindrome_Number.cpp
File metadata and controls
33 lines (32 loc) · 826 Bytes
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
class Solution {
public:
/*
// recursive solution
void isPalindrome(int x, int &y, bool &result) {
if(!result or x == 0) return;
int a = x % 10;
isPalindrome(x / 10, y, result);
if(!result) return;
int b = y % 10;
if(a != b) result = false;
y /= 10;
}
bool isPalindrome(int x) {
if(x < 0) return false;
bool result = true;
int y = x;
isPalindrome(x, y, result);
return result;
}
*/
bool isPalindrome(int x) {
int reverseX = 0;
int X = x;
while(x) {
reverseX = reverseX * 10 + x % 10;
x /= 10;
}
return X == reverseX and
X >= 0; // if the number is negative, its not palindrome
}
};