小白笔记------------------------leetcode(7. Reverse Integer)
2016-12-24 14:36
459 查看
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 class Solution {
public int reverse(int x) {
String str = Integer.toString(x);
long result = 0;
int i = str.length() - 1;
int sign = 1;
if(str.charAt(0) == '-'){
sign = -1;
while(i >= 1 ){
result = result * 10 + (str.charAt(i) - '0');
i--;
}
}else{
sign = 1;
while(i >= 0 ){
result = result * 10 + (str.charAt(i) - '0');
i--;
}
}
if(sign == -1){
result = -result;
}
if(result > Integer.MAX_VALUE || result < Integer.MIN_VALUE){
return 0;
}
return (int)result;
}
}
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 class Solution {
public int reverse(int x) {
String str = Integer.toString(x);
long result = 0;
int i = str.length() - 1;
int sign = 1;
if(str.charAt(0) == '-'){
sign = -1;
while(i >= 1 ){
result = result * 10 + (str.charAt(i) - '0');
i--;
}
}else{
sign = 1;
while(i >= 0 ){
result = result * 10 + (str.charAt(i) - '0');
i--;
}
}
if(sign == -1){
result = -result;
}
if(result > Integer.MAX_VALUE || result < Integer.MIN_VALUE){
return 0;
}
return (int)result;
}
}
相关文章推荐
- 小白笔记-----------------------leetcode(8. String to Integer (atoi) )
- LeetCode——7. Reverse Integer
- 小白笔记----------------------leetcode(100 Same Tree)
- leetcode 7. Reverse Integer
- 小白笔记----------------------------------------leetcode(40. Combination Sum II )
- leetcode-7. Reverse Integer
- [Leetcode] 7. Reverse Integer
- LeetCode-7. Reverse Integer
- leetcode7. Reverse Integer
- [LeetCode]7. Reverse Integer
- leetcode7. Reverse Integer
- LeetCode 7. Reverse Integer
- LeetCode---7. Reverse Integer
- LeetCode笔记:8. String to Integer (atoi)
- LeetCode 7. Reverse Integer C++--带正负号数字反转
- LeetCode 7. Reverse Integer
- LeetCode 7. Reverse Integer
- 【LeetCode】7. Reverse Integer
- 【LeetCode】Reverse Integer解题笔记
- python写算法题:leetcode: 7. Reverse Integer