LeetCode刷题--回文数

题目:

LeetCode刷题--回文数_第1张图片

解题思路:

1.如何反转整数?

2.负数处理

3.数据会不会溢出?

 

c语言解法:

bool isPalindrome(int x)
{
    //负数不是回文数
    if(x < 0)  
    {
        return false;
    }

    int tmp1 = x;
    long tmp2 = 0;  //int类型会导致数据溢出

    while(tmp1)
    {
        tmp2 = tmp2 * 10 + tmp1 % 10;
        tmp1 = tmp1 / 10;
    }

    return (tmp2 == x);
}

 

你可能感兴趣的:(LeetCode)