/*本解法参照于《Leetcode题解》(网址:https://github.com/soulmachine/leetcode),
或作修改,或增加注释等。*/
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0) return false;//负数不是回文
int d(1);
while(x / d >= 10) d *= 10;//获得与x同样位数的最小数
while(x){
int f = x/d;
int l = x%10;
if(f != l) return false;//对应两个数字不相等,不是回文
x = x%d/10;
d /= 100;
}
return true;//所有对应数字是相等的,是回文
}
};Leetcode之Palindrome Number
最新推荐文章于 2020-08-18 15:23:01 发布
本文介绍了一种用于判断整数是否为回文的有效算法。该算法通过将整数的高位数字与低位数字进行对比来确定其是否为回文数,并在过程中避免了直接反转整数,从而提高了效率。
176

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



