LeetCode | Palindrome Number

题目:

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


思路:

翻转数字并与原数字比较。

代码:

class Solution {
public:
    bool isPalindrome(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(x < 0)
        {
            return false;
        }
        else if(x == 0)
        {
            return true;
        }
        if(x == reverse(x))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    int reverse(int x)
    {
        int res = 0;
        while(x > 0)
        {
            res = res * 10 + x %10;
            x /= 10;
        }
        return res;
    }
};
    原文作者:Allanxl
    原文地址: https://blog.csdn.net/lanxu_yy/article/details/11901361
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞