Wednesday, January 7, 2015

LeetCode 9: Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.
public class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0)
            return false;
            
        if (x < 10)
            return true;
        
        // We should firstly convert the number to String    
        String s = String.valueOf(x);
        int n = s.length();
            
        for (int i = 0; i < n/2; i++)
            if (s.charAt(i) != s.charAt(n-i-1))
                return false;
                
        return true;
    }
}

No comments:

Post a Comment