FANGYEFENG

Mar 08, 2024

9. Palindrome Number

Given an integer x, return true if x is a palindrome, and false otherwise.


Solution 1

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool isPalindrome(int x) {
if(x<0) return false;
int t=x;
long long y=0;
while(t>0){
y=y*10+t%10;
t/=10;
}
return x==y;
}
};

Solution 2

C++
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
bool isPalindrome(int x) {
auto s=to_string(x);
int i=0,j=s.size()-1;
while(i<j && (s[i]==s[j])){
i++;
j--;
}
return i==j || i-j==1;
}
};
OLDER > < NEWER