leetcode: Palindrome Number

http://oj.leetcode.com/problems/palindrome-number/

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

click to show spoilers.

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.

思路

参考Reverse Integer的实现,溢出问题不用担心,如果溢出了那显然不是回文数。

 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         if (x < 0) {
 5             return false;
 6         }
 7         
 8         int y = x, z = 0;
 9         while (y > 0) {
10             z = z * 10 + y % 10;
11             y /= 10;
12         }
13         
14         return z == x;
15     }
16 };
原文地址:https://www.cnblogs.com/panda_lin/p/palindrome_number.html