7. Reverse Integer--数字反转输出
2017-06-01 14:59
381 查看
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.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
这道题目的意思是:输入一个整数,输出它反转之后的数字,如果反转后的数字溢出,则返回0;思路如下:用取余的方式取出输入整数的每一位,然后将取出的数字用newResult(即第n-1次循环取出的数)保存,然后将newResult*10加上tail(第n次循环取出的数),正常情况下,(newResult-tail)/10=result,如果result*10+tail溢出,即大于Integer.MAX_VALUE(2147483647)或小于Integer.MIN_VALUE(-2147483648)时,newResult只能取result*10+tail的低32位,因此这时(newResult-tail)/10!=result,则返回0。
代码如下:
int result = 0;
while (x != 0)
4000
{
int tail = x % 10;
int newResult = result * 10 + tail;
if ((newResult-tail)/10!=result)
{ return 0; }
//保存中间结果
result = newResult;
x = x / 10;
}
return 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.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
这道题目的意思是:输入一个整数,输出它反转之后的数字,如果反转后的数字溢出,则返回0;思路如下:用取余的方式取出输入整数的每一位,然后将取出的数字用newResult(即第n-1次循环取出的数)保存,然后将newResult*10加上tail(第n次循环取出的数),正常情况下,(newResult-tail)/10=result,如果result*10+tail溢出,即大于Integer.MAX_VALUE(2147483647)或小于Integer.MIN_VALUE(-2147483648)时,newResult只能取result*10+tail的低32位,因此这时(newResult-tail)/10!=result,则返回0。
代码如下:
int result = 0;
while (x != 0)
4000
{
int tail = x % 10;
int newResult = result * 10 + tail;
if ((newResult-tail)/10!=result)
{ return 0; }
//保存中间结果
result = newResult;
x = x / 10;
}
return result;
相关文章推荐
- LeetCode 7. Reverse Integer 倒序输出数字
- leetcode_7. Reverse Integer 反转整数的各个数字
- leetCode 7. Reverse Integer (数字反转) 解题思路与方法
- LeetCode 7. Reverse Integer C++--带正负号数字反转
- leetcode 7. Reverse Integer-数字翻转
- LeetCode 7. Reverse Integer(整数反转)
- 第三周Reverse Integer反转数字
- 一个有趣的的数字反转输出
- 输入一个整数,将各位数字反转后输出
- 剑指offer 01-06解答思路以及代码(顺序数组找特定数字,替换空格字符,链表反转输出,重建二叉树,两个栈实现队列效果,旋转数组最小元素)
- leetcode——Reverse Integer 反转整数数字(AC)
- Leetcode 7 Reverse Integer 反转数字
- LeetCode | 7. Reverse Integer 数字反向循环技巧题
- 7. Reverse Integer(整数反转,注意超范围记为0)
- 7. Reverse Integer(反转整数)
- java 输入一个数字,反转(倒置)输出这个数字的值
- 算法练习7. Reverse Integer 把整数反向输出
- Leetcode 07 Reverse Integer(数字反转) && 09 Palindrome number(回文判断)
- Reverse Integer (数字反转)
- LeetCode 7 Reverse Integer(反转数字)