package com.ikouz.study;

/**
 * Created by liujiaming on 2016/07/20 22:58.
 */
public class Math_9_PalindromeNumber {

    /**
     * Determine whether an integer is a palindrome. Do this without extra space.
     * <p/>
     * 总结：注意去掉首末位循环时，i = i - 2;
     */
    public static boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        int i = -1, tmp = x, a, b, k;
        while (tmp != 0) { // 获取位数
            tmp = tmp / (10);
            i++;
        }
        tmp = x;
        while (tmp < -9 || tmp > 9) {
            a = tmp % 10;
            k = (int) Math.pow(10.0, (double) i);
            b = tmp / k;
            if (a != b) {
                return false;
            }
            i -= 2;
            tmp = (tmp - b * k) / 10; // 去掉首末位
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isPalindrome(1000021));
        System.out.println(isPalindrome(10));
        System.out.println(isPalindrome(-2147447412));
    }
}

@@ -229,41 +229,360 @@
  %E6%80%BB%E7%BB%93%EF%BC%9A
-%E6%B3%A8%E6%84%8F%E5%8E%BB%E6%8E%89%E9%A6%96%E6%9C%AB%E4%BD%8D%E5%BE%AA%E7%8E%AF%E6%97%B6%EF%BC%8Ci = i - 2;%0A     */%0A   
+%E5%8F%8D%E6%AD%A3%E5%90%8E%E6%98%AF%E5%90%A6%E7%9B%B8%E7%AD%89%0A     */%0A    public static boolean isPalindrome(int x) %7B%0A        if (x %3C 0) %7B%0A            return false;%0A        %7D%0A%0A        int y = 0, tmp = x;%0A        while (tmp != 0) %7B%0A            y = y * 10 + tmp %25 10;%0A            tmp /= 10;%0A        %7D%0A        return y == x;%0A    %7D%0A%0A    /**%0A     * %E6%80%BB%E7%BB%93%EF%BC%9A%E5%8E%BB%E6%8E%89%E9%A6%96%E6%9C%AB%E4%BD%8D%E7%9A%84%E6%96%B9%E5%BC%8F%E8%A1%8C%E4%B8%8D%E9%80%9A%EF%BC%8C%E8%A7%A3%E5%86%B3%E4%B8%8D%E4%BA%86 1021 %E8%BF%99%E7%A7%8D%E5%8E%BB%E6%8E%89%E9%A6%96%E6%9C%AB%E5%90%8E%E4%B8%A2%E6%8E%890%E7%9A%84%E6%83%85%E5%86%B5%0A     */%0A    @Deprecated
  pub
@@ -604,32 +604,34 @@
 ean isPalindrome
+_1
 (int x) %7B%0A      
@@ -1230,18 +1230,16 @@
 rome(100
-00
 21));%0A  
@@ -1277,17 +1277,25 @@
 ndrome(1
-0
+119123219
 ));%0A    

solution of 9