Palindrome Number

题目描述:

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

solution1:              //依次比较高位和低位数字是否相同

bool isPalindrome(int x) {
    if(x < 0)
        return false;
    int div = 1;
    while(x / div >= 10)
        div *= 10;
    while (x != 0)
    {
        int left = x / div;
        int right = x % 10;
        if(left != right)
            return false;
        x = (x % div) / 10;
        div /= 100;
    }
    return true;
}

solution2:              //利用回文特性,比较反转后的整数和初始整数是否相同

bool isPalindrome(int x) {
    if(x < 0)
        return false;
    long long reverse = 0;
    int origin = x; 
    while(x != 0)
    {
        reverse = reverse * 10 + x % 10;
        x /= 10;
    }
    return reverse == origin;
}

ps:

    细心做题,稳扎稳打。

posted @ 2015-01-16 10:05  Sawyer Ford  阅读(190)  评论(0编辑  收藏  举报