Determine whether an integer is a palindrome. Do this without extra space.
Python
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x < 0:
return bool(0)
x_str = str(x)
if len(x_str) % 2 == 0:
if x_str[:len(x_str)/2] == x_str[:len(x_str)/2 - 1:-1]:
return bool(1)
else:
return bool(0)
else:
if x_str[:len(x_str)/2] == x_str[:len(x_str)/2:-1]:
return bool(1)
else:
return bool(0)
Java
class Solution {
public boolean isPalindrome(int x) {
char[] x_char= String.valueOf(x).toCharArray();
boolean result = true;
for(int i = 0, j = x_char.length - 1; i <= x_char.length && j >= i; i ++, j--){
if (x_char[i] != x_char[j]) {
result = false;
return result;
}
}
return result;
}
}