Palindrome Number

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

判断一个integer是否为回文。注意不要用额外的空间。

另外,注意考虑负数。

 

 1 public class Solution {
 2      public boolean isPalindrome(int x) {
 3         if(x<0){
 4             return false;
 5         }
 6         
 7         int count=0;
 8         int xx = x;
 9          while(x!=0) {  
10                 x=x / 10;  
11                 count++;
12             }  
13     //     System.out.println(count);
14          
15          int[] arr = new int[count];
16          
17          for(int i=0;i<count;i++) {  
18              arr[i]=xx % 10;  
19               
20               xx /= 10;
21             //  System.out.println(arr[i]); 
22             }  
23          
24              int low = 0;
25             int high = count - 1;
26             while (low < high) {
27               if (arr[low] != arr[high]){
28                 return false; // Not a palindrome
29               }
30               low++;
31               high--;
32             }
33 
34             return true; //is a palindrome
35         }
36 }

 532 ms.

posted @ 2015-08-08 10:26  codingcat  阅读(165)  评论(0编辑  收藏  举报