Last active
August 29, 2015 13:56
-
-
Save Jeffwan/8886595 to your computer and use it in GitHub Desktop.
Reverse digits of an integer. @leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package leetcode.simpleCoding; | |
| /** | |
| * @author jeffwan | |
| * @date Feb 8, 2014 | |
| * | |
| * Objective: write the simple and readable codes | |
| * There's no algorithm in this kind of problem but some tiny simple method. Our goal is to make it as clear as possible. | |
| * In addition, It's a good opportunity communicatting with interviewer about the overflow, 100 -- 001 (display). | |
| * | |
| * It's better to throw exception but not return -1 (Ambiguity) | |
| */ | |
| public class ReverseInteger { | |
| public static void main(String args[]) { | |
| // System.out.println(reverse3(1000000003)); overflow | |
| System.out.println(reverse(1000000003)); | |
| // System.out.println(Integer.MAX_VALUE); | |
| // System.out.println(Integer.MIN_VALUE); | |
| } | |
| /** | |
| * Actually, we don't need to concern if a number if positive or not. It will always take the operator during operation. | |
| * If we don't care overflow problem, this solution will be very short. | |
| */ | |
| public static int reverse(int x) { | |
| int result = 0; | |
| int originalX = x; | |
| while (x != 0) { | |
| result = result * 10 + x % 10; | |
| x = x /10; | |
| } | |
| if ((originalX > 0 && result < 0) || (originalX < 0 && result > 0)) { | |
| return -1; | |
| } | |
| return result; | |
| } | |
| /** | |
| * Use mod to get every number and then reverse | |
| * Just use int without judging x == 0 | |
| */ | |
| public static int reverse2(int x) { | |
| boolean isNegative = x < 0 ? true: false; | |
| x = Math.abs(x); | |
| int result = 0; | |
| while (x > 0) { | |
| result = result * 10 + x % 10; | |
| x = x / 10; | |
| } | |
| if (result < 0) { // no multioverflow? | |
| return -1; | |
| } | |
| if (isNegative) { | |
| return -result; | |
| } | |
| return result; | |
| } | |
| /** | |
| * Convert integer to string, use charAt changing brutely | |
| * Here I rely on function Integer.valueOf to solve the problem 100 --> 1 but not 001 | |
| * Didn't sovle overflow problem -- but leetcode online judge accepted? can't believe | |
| * Overflow will remains in Integer.valueOf() which troughs exception that we can't handle like reverse2() | |
| */ | |
| public static int reverse3(int x) { | |
| if (x == 0) return 0; | |
| boolean isPositive = true; | |
| if (x < 0) { | |
| isPositive = false; | |
| } | |
| String str = Math.abs(x) + ""; | |
| StringBuffer sb = new StringBuffer(); | |
| for (int i = str.length() - 1; i >= 0; i--) { | |
| sb.append(str.charAt(i)); | |
| } | |
| int result = Integer.valueOf(sb.toString()); | |
| if(isPositive) { | |
| return result; | |
| } else { | |
| return -result; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment