题目描述:
Determine whether an integer is a palindrome. Do this without extra space.
思路:检测是不是回文数。将给定的数翻转即可。与第七题的思路相同,网上给的解法是从头和尾同时遍历,比较是否相等,相等则删除继续比较,直到最中间一位。
1 public class Solution9 { 2 public boolean isPalindrome(int x){ 3 int result = 0; 4 int a = x; 5 while(a>0){ 6 result = result*10+a%10; 7 a/=10; 8 } 9 System.out.println(result); 10 if(result == x){ 11 return true; 12 } 13 return false; 14 } 15 public static void main(String[] args) { 16 // TODO Auto-generated method stub 17 Solution9 solution9 = new Solution9(); 18 System.out.println(solution9.isPalindrome(-2147483648)); 19 } 20 21 }