Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Example2: x = -123, return -321
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
This file contains 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; | |
} | |
} | |
} |
No comments:
Post a Comment