Determine whether an integer is a palindrome. Do this without extra space.
方法:数字前后位分别提取并进行判断是否相等。
class Solution {
public:
bool isPalindrome(
int x) {
if(x<
0)
return false;
int y = x;
int len =
1;
while(y/
10!=
0){
len++;
y/=
10;
}
bool isValid ;
int left = len-
1 ,right =
1;
while(left >= right){
int big = x /
static_cast<
int>(
pow(
10,left)) %
10;
int small = x %
static_cast<
int>((
pow(
10,right)))/
pow(
10,right-
1);
if(big != small)
return false;
--left;
++right;
}
return true;
}
};
转载请注明原文地址: https://ju.6miu.com/read-665942.html