Palindrome Number

https://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.

 1 public class Solution {
 2     public static boolean isPalindrome(int x) {
 3     if(x<0){return false;}
 4         int len=0;
 5         String maxint=""+Integer.MAX_VALUE;
 6         int maxlen=maxint.length();
 7         if(x>=Math.pow(10,maxlen-1)||x<=-Math.pow(10, maxlen-1)){len=maxlen;}
 8         else{while((x/(int)Math.pow(10,++len))!=0);}
 9         for(int i=1;i<=len/2;i++){
10             int a=(int) (x%(Math.pow(10, i))/(Math.pow(10, i-1)));
11             int b=(int) (x%(Math.pow(10, len-i+1))/(Math.pow(10, len-i)));
12             if(a!=b){return false;}
13         }
14         return true;
15     }
16     public static void main(String[]args){
17     System.out.println(isPalindrome(-2147483648));
18     }
19 }
原文地址:https://www.cnblogs.com/qq1029579233/p/4477115.html