19.1.25 [LeetCode9]Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?

 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         if (x < 0)return false;
 5         int y = 0, _x = x;
 6         while (_x) {
 7             y *= 10;
 8             y += _x % 10;
 9             _x /= 10;
10         }
11         return (x == y);
12     }
13 };
View Code

 

posted @ 2019-01-25 10:51  TobicYAL  阅读(142)  评论(0编辑  收藏  举报