Determine whether an integer is a palindrome. Do this without extra space.
1 public class Solution { 2 public boolean isPalindrome(int x) { 3 if (x < 0) { 4 return false; 5 } else if (x < 10) { 6 return true; 7 } 8 int temp = x / 10; 9 int h = 10; 10 while (temp >= 10) { 11 temp /= 10; 12 h *= 10; 13 } 14 while (h > 1) { 15 int r = x % 10; 16 int l = x / h; 17 if (l != r) { 18 return false; 19 } 20 x %= h; 21 x /= 10; 22 h /= 100; 23 } 24 return true; 25 } 26 }

浙公网安备 33010602011771号