Leetcode Problem9

Palindrome Number

回文数问题。本题要求不能使用string来转换。但其实我一开始想的是用取余的方法来求取每个位的数字,通过数组储存起来,然后再比较数组对应位置的数字看是否相同即可。

在别的博客上看到了另外一种解法,就是求出该数字的反转数,看两个数是否相等。

bool isPalindrome(int x)
{
    if(x<0) 
        return false;
    else if(x==0)
        return true;
    int num[10],count=0;
    while(x>0)
    {
        num[count++]=x%10;
        x=x/10;
    }
    for(int i=0;i<=count/2;i++)
    {
        if(num[i]!=num[count-1-i])
            return false;
    }
    return true;
}

猜你喜欢

转载自blog.csdn.net/vandance/article/details/81486741
今日推荐