491 · Palindrome Number - LintCode

# Description

Check a positive number is a palindrome or not.

A palindrome number is that if you reverse the whole number you will get exactly the same number.

It's guaranteed the input number is a 32-bit integer, but after reversion, the number may exceed the 32-bit integer.

# Example

Example 1:

Input:11
Output:true

Example 2:

Input:1232
Output:false
Explanation:
1232!=2321

# Code

public class Solution {
    /**
     * @param num: a positive number
     * @return: true if it's a palindrome or false
     */
    public boolean isPalindrome(int num) {
    
      String newX = x+"";
        StringBuilder res = new StringBuilder();
        for (int i = newX.length() - 1; i >= 0; i--) {
            res.append(newX.charAt(i));
        }
        
        return res.toString().equals(newX);     
    }
}

class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0) return false;
        StringBuilder s = new StringBuilder();
        s.append(x);
        int left = 0;
        int right = s.length() - 1;
        while (left < right) {
            if (s.charAt(left) != s.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}