Sunday, March 20, 2016

LeetCode 刷题第一阶段 Palindrome Number

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

======================================================

Just create a reverse int and compare them. Pay attention that if the input is negative it will always return false.

Code:
public class Solution {
    public boolean isPalindrome(int x) {
        boolean ret = true;
        if(x < 0) return false;
        int rev = 0;
        int n = x;
        while(n != 0){
            int temp = n % 10;
            rev = rev * 10 + temp;
            n = n / 10;
        }
        
        if(rev != x) ret = false;
        
        return ret;
    }

}

No comments:

Post a Comment