Determine whether an integer is a palindrome. Do this without extra space.
1刷
題解:創(chuàng)建一個(gè)新的數(shù)字谨湘,從尾部開始一個(gè)digit一個(gè)digit構(gòu)造數(shù)字, 直至median,
最終返回時(shí)判斷是否能得到兩個(gè)相同的數(shù)字
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);
}
}