2015年7月30日 星期四

LEET code--Palindrome Number

Palindrome Number

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

簡單  把之前寫好的反轉int直接拿來用

會發現不會過
原來負數都不算
是負的直接略過


int reverse(int x) {
    int ans=0;
    while(x!=0){
        if(ans > INT_MAX/10 || ans< INT_MIN/10 )return 0;
        ans*=10;
        ans+=x%10;
        x/=10;
    }
    return ans;
}

bool isPalindrome(int x) {
    if (x < 0) {
        return false;
    }
    return x==reverse(x) ? true:false;
}

沒有留言:

張貼留言