class Solution {
public boolean isPalindrome(int x) {
// first instinct, parse into String, reverse, check is it the same
String s = String.valueOf(x);
char c[] = s.toCharArray();
for (int i = 0; i < c.length/2; i++) {
char temp = c[i];
c[i] = c[c.length - i - 1];
c[c.length - i - 1] = temp;
}
return String.valueOf(c).equals(s);
}
}
Math
time: O(logn), log base is 10
space: O(1)
class Solution {
public boolean isPalindrome(int x) {
if (x < 0) return false;
int y = x;
int res = 0;
while (y != 0) {
res = res*10 + y%10;
y /= 10;
}
return res == x;
}
}