Leetcode - Reverse Integer
2015-01-08 04:28
330 查看
public class Solution {
public int reverse(int x) {
if( x == Integer.MIN_VALUE)
return 0;
int a=Math.abs(x);
int[] arr = new int[Integer.toString(a).length()];
for(int i=0; i<arr.length; i++){
arr[i]=a%10;
a /= 10;
}
int val=0;
for(int i=0; i<arr.length; i++){
if(arr[arr.length-i-1]*Math.pow(10, i-1)+val/10 > Integer.MAX_VALUE/10 ||
arr[arr.length-i-1]*Math.pow(10, i-1)+val/10 == Integer.MAX_VALUE/10 && arr[arr.length-1]>7)
return 0;
val += arr[arr.length-i-1]*Math.pow(10, i);
}
return x<0 ? -1*val: val;
}
}
--------------------
HINT:
判断正负数越界~~~~
===============
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
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?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
public int reverse(int x) {
if( x == Integer.MIN_VALUE)
return 0;
int a=Math.abs(x);
int[] arr = new int[Integer.toString(a).length()];
for(int i=0; i<arr.length; i++){
arr[i]=a%10;
a /= 10;
}
int val=0;
for(int i=0; i<arr.length; i++){
if(arr[arr.length-i-1]*Math.pow(10, i-1)+val/10 > Integer.MAX_VALUE/10 ||
arr[arr.length-i-1]*Math.pow(10, i-1)+val/10 == Integer.MAX_VALUE/10 && arr[arr.length-1]>7)
return 0;
val += arr[arr.length-i-1]*Math.pow(10, i);
}
return x<0 ? -1*val: val;
}
}
--------------------
HINT:
判断正负数越界~~~~
===============
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
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?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
相关文章推荐
- leetcode Reverse Integer
- LeetCode编程练习 - Reverse Integer学习心得
- Leetcode Reverse Integer
- LeetCode:Reverse Integer
- LeetCode之Reverse Integer
- Leetcode_Reverse Integer(考虑了溢出情况)
- leetcode: reverse-integer
- LeetCode之Reverse Integer
- [Leetcode] Reverse Integer
- LeetCode:Reverse Integer
- leetcode: Reverse Integer
- LeetCode : 7 Reverse Integer C++
- leetcode 7 Reverse Integer
- LeetCode-7(Reverse Integer)
- leetcode Reverse Integer
- LeetCode (Easy Part) Reverse Integer
- Reverse Integer [LeetCode]
- leetcode Reverse Integer
- 【leetcode】Reverse Integer整数反转----Java代码实现
- Reverse Integer LeetCode 第七题