leetcode 回文数 简单
这个题与https://www.cnblogs.com/rookie-acmer/p/15041965.html此题不同,不能先翻转得到一个数,再进行对比,因为即使翻转后会溢出,它也完全满足回文数的条件。
代码:
class Solution { public: bool isPalindrome(int x) { if(x >= 0 && x < 10) return true; if(x < 0 || x % 10 == 0) return false; int y = 0; while(x != 0) { y = y * 10 + x % 10; x /= 10; if(y >= x) break; } return x == y || x == y / 10; } };
值得注意的有一点,0 <= x < 10,需要最先进行判断,其次才是 x < 0 || x % 10 == 0 的情况,因为 0 % 10 == 0