leetcode刷题,总结,记录,备忘 9

leetcode9,Palindrome Number

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

click to show spoilers.

Subscribe to see which companies asked this question

数字回文,先求出数字的位数,然后从左右开始,一个一个求 每位上的数字,判断是否相等

class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0)
        {
            return false;
        }
        if (x < 10)
        {
            return true;
        }
        
        int src = x;
        int temp = 1;
        while (1)
        {
            if (x/10 != 0)
            {
                temp *= 10;
                x /= 10;
            }
            else
            {
                break;
            }
        }
        
        while (1)
        {
            int left = src / temp;
            int right = src % 10;
            if (left != right)
            {
                return false;
            }
            else
            {
                src %= temp;
                src /= 10;
                temp /= 100;
                if (src == 0)
                {
                    break;
                }
            }
        }
        
        return true;
    }
};


你可能感兴趣的:(leetcode刷题,总结,记录,备忘 9)