leetcodeブラシ問題、まとめ、記録、メモ9

1214 ワード

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
数字の回文、まず数字の桁数を求めて、それから左右から、1つ1つの上の数字を求めて、等しいかどうかを判断します
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;
    }
};