Determine whether an integer is a palindrome. Do this without extra space.
1刷
題解:創建一個新的數字,從尾部開始一個digit一個digit構造數字, 直至median,
最終返回時判斷是否能得到兩個相同的數字
Time Complexity - O(logx), Space Complexity - O(1)。
public class Solution {
public boolean isPalindrome(int x) {
if(x < 0 || (x!=0 && x%10==0)) return false;
int res = 0;
while(x>res){
res = res*10 + x%10;
x /=10;
}
return (x == res || x == res/10);
}
}