Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
解题思路:由于不能有额外的空间,所以可以参考上面的第7题倒转数字,根据回文的性质,倒转后的数字=原数字,注意倒转过程中不要使用额外的空间(除了必要的res)。
代码如下:
class Solution {
public:
bool isPalindrome(int x) {
if(x<0) return false;
int res=0,temp=x;
while(temp>0){
res=res*10+temp%10;
temp=temp/10;
}
if(res==x){
return true;
}else{
return false;
}
}
};
本文介绍了一种在不使用额外空间的情况下判断一个整数是否为回文数的方法。通过将整数反转并与原数对比来实现,适用于算法面试及日常编程。
5941

被折叠的 条评论
为什么被折叠?



