果果1020

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

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 }

 

posted on 2017-01-02 15:12  果果1020  阅读(87)  评论(0)    收藏  举报