自己调试,写出来的一道题目诶,虽然是Easy难度的,但是也很开心了。
注意:
不要连续 if..if..if,容易导致连续满足条件,要用if...else if...else if...else if 或 if...else if...else。
如果有溢出,直接返回0。
步骤:
1.如果是负数,直接返回false。
2.如果是0或者个位数,直接返回true。
3.其他情况,进行判断。抽象出一个reverse_num的函数。
class Solution { public: bool isPalindrome(int x) { if(x < 0) return false; else if( x <= 9) return true; else { return ( x == reverse_num(x))? true : false; } return 0; } int reverse_num(int x){ int base = 0; while( x > 0){ if(base > INT_MAX / 10 || (base == INT_MAX / 10 && x % 10 > 7) ) return 0; base = base * 10 + x % 10; x /= 10; } return base; } };