package xm.math.leetcode;

import org.junit.Test;

/**
 * Reverse digits of an integer.
 * <p>
 * Example1: x = 123, return 321
 * Example2: x = -123, return -321
 *
 * @author XuMing
 */
public class ReverseInteger {
    public int reverse(int x) {
        int result = 0;
        while (x != 0) {
            int a = x % 10;
            int b = result * 10 + a;
            int c = (b - a) / 10;
            if (c != result) {
                return 0;
            }
            result = b;
            x = x / 10;
        }
        return result;
    }

    @Test
    public void test() {
        ReverseInteger reverseInteger = new ReverseInteger();
        System.out.println(reverseInteger.reverse(-123));
    }
}

@@ -44,16 +44,65 @@
 .Test;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * R
@@ -760,10 +760,1865 @@
 ;%0A    %7D%0A
-%7D
+%0A    public void test2() %7B%0A        String q = %22how  are   you%22;%0A        String%5B%5D temp = (String%5B%5D) q.split(%22 %22);%0A        for (int i = temp.length; i %3E 0; i--) %7B%0A            System.out.print(temp%5Bi - 1%5D + %22 %22);%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void test1() %7B%0A        ReverseInteger reverseInteger = new ReverseInteger();%0A        reverseInteger.test2();%0A    %7D%0A%0A    @Test%0A    public void test3() %7B%0A        ReverseInteger reverseInteger = new ReverseInteger();%0A        reverseInteger.test31();%0A    %7D%0A%0A    public void test31() %7B%0A        String input = %22how  are   you%22;%0A        String str = %22how  are   you%22;%0A        String%5B%5D array = str.split(%22 %22);%0A        int len = array.length;%0A        for (int i = 0; i %3C len / 2; i++) %7B%0A            String temp = array%5Bi%5D;%0A            array%5Bi%5D = array%5Blen - 1 - i%5D;%0A            array%5Blen - 1 - i%5D = temp;%0A        %7D%0A        String result = %22%22;%0A        boolean isFirst = true;%0A        for (String s : array) %7B%0A            if (isFirst) %7B%0A                isFirst = false;%0A            %7D else %7B%0A                result += %22 %22;%0A            %7D%0A            result += s;%0A        %7D%0A        System.out.println(result);%0A    %7D%0A%0A    public void test4() %7B%0A        String q = %22how  are   you%22;%0A        Map map = new HashMap%3C%3E();%0A        int count = 0;%0A        for (int i = 0; i %3C q.length(); i++) %7B%0A            if (q.indexOf(%22  %22) %3E -1) %7B%0A                map.put(%22  %22, i);%0A            %7D%0A            if (q.indexOf(%22   %22) %3E -1) %7B%0A                map.put(%22   %22, i);%0A            %7D%0A        %7D%0A        String%5B%5D temp = (String%5B%5D) q.split(%22 %22);%0A        for (int i = temp.length; i %3E 0; i--) %7B%0A            if (temp%5Bi - 1%5D.indexOf(%22 %22) %3E -1)%0A                System.out.print(temp%5Bi - 1%5D);%0A        %7D%0A%0A    %7D%0A%0A    @Test%0A    public void test44() %7B%0A        ReverseInteger reverseInteger = new ReverseInteger();%0A        reverseInteger.test4();%0A    %7D%0A%7D%0A%0A
 %0A

添加revert答案。xuming 20170616