@@ -0,0 +1,1299 @@
+package com.pengyifan.leetcode;%0A%0A/**%0A * Reverse digits of an integer.%0A * %3Cp%3E%0A * Example1: x = 123, return 321%0A * %3Cp%3E%0A * Example2: x = -123, return -321%0A * %3Cp%3E%0A * Have you thought about this?%0A * %3Cp%3E%0A * Here are some good questions to ask before coding. Bonus points for you if%0A * you have already thought through this!%0A * %3Cp%3E%0A * If the integer's last digit is 0, what should the output be? ie, cases such%0A * as 10, 100.%0A * %3Cp%3E%0A * Did you notice that the reversed integer might overflow? Assume the input is%0A * a 32-bit integer, then the reverse of 1000000003 overflows. How should you%0A * handle such cases?%0A * %3Cp%3E%0A * Throw an exception? Good, but what if throwing an exception is not an%0A * option? You would then have to re-design the function (ie, add an extra%0A * parameter).%0A */%0Apublic class ReverseInteger %7B%0A%0A  public int reverse(int x) %7B%0A    boolean neg = x %3C 0 ? true : false;%0A    if (neg) %7B%0A      x = -1 * x;%0A    %7D%0A    int rev = 0;%0A    while (x %3E 0) %7B%0A      int d = x %25 10;%0A      rev = 10 * rev + d;%0A      x /= 10;%0A    %7D%0A    if (neg) %7B%0A      rev = -1 * rev;%0A      if (rev %3E= 0) %7B%0A        // overflow%0A        throw new RuntimeException(%22overflow%22);%0A      %7D%0A    %7D else %7B%0A      if (rev %3C 0) %7B%0A        // overflow%0A        throw new RuntimeException(%22overflow%22);%0A      %7D%0A    %7D%0A    return rev;%0A  %7D%0A%7D%0A

reverse integer