[LeetCode] 判断一个整数是否是回文数

问题描述:

[LeetCode] 判断一个整数是否是回文数_第1张图片

算法思路:

我们可以求出该整数翻转后的数值,比较是否与待判断整数相等。如果相等,则是回文数,否则不是。

C++实现:

class Solution {
public:
    bool isPalindrome(int x) {
 
        int sum = 0;
        int temp = x;
        if(x < 0)
        {
            return false;
        }
 
        while(x)
        {
            sum = sum * 10 + x % 10;
            x /= 10;
        }
 
        if(temp == sum)
        {
            return true;
        }
        else
        {
            return false;
        }
        
    }
};

不足:反转后的整数可能存在溢出问题

见:signed integer overflow

你可能感兴趣的:(LeetCode)