Leetcode 9. Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

Next challenges: Palindrome Linked List

代码:

 1 public class Solution {
 2     public boolean isPalindrome(int x) {
 3         // 负数和末尾是0的数(不包括0)都不符合
 4         if(x < 0 || (x % 10 == 0 && x != 0)) return false;
 5         // 将数的十进制表示从中截半,例如12521截半就是12和21
 6         int rx = 0;
 7         while(x > rx) {
 8             rx = rx * 10 + x % 10;
 9             x /= 10;
10         }
11         return rx == x || rx / 10 == x;
12     }
13 }
原文地址:https://www.cnblogs.com/Deribs4/p/7216781.html