【leetcode】6 Palindrome Number

判断是否为回文数

注意事项;

1 负数不是回文数

2 0是回文数

3 如果按照reverse integer,会产生溢出

4 如果int x 转换为string,则另辟空间,与题目不符

5 取头尾两数,比较元素是否相等;

根据x获得x的位数,以此为基准进行取位操作,每次循环后基准除以100,因为每次操作比较两元素,后面比较会删除这两元素

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0)
            return false;
        if(x==0)
            return true;
           
        int pivot=1;
        while(x/pivot>=10)
            pivot*=10;
        int left=1,right=1;
        while(x){
            left=x/pivot;
            right=x%10;
            if(left!=right)
                return false;
            x=x-pivot*left;
            pivot=pivot/100;
            x=x/10;
        }
            return true;
       
        
    }
};

原文地址:https://www.cnblogs.com/wygyxrssxz/p/4493178.html