6_leetcode_Palindrome Number

来源:互联网 发布:隐藏域名ip cdn 编辑:程序博客网 时间:2024/05/18 01:24

Determine whether an integer is a palindrome. Do this without extra space

1:给出数字是负数以及个位数的时候。2:分别从前到后和从后到前获得最高位和相应最低位,比较两者的大小, 如果都分别相等,则是回文数

    bool isPalindrome(int x)    {        if( x < 0)            return false;        if( x < 10)            return true;                bool result = false;                long long left = 1;        int right = 1;                while(x / (left * 10))        {            left *= 10;        }                while(left > right && (x / left) % 10 == (x / right) % 10)        {            left = left / 10;            right = right * 10;        }                if(left <= right)        {            result = true;        }                return result;    }


0 0
原创粉丝点击