Reverse Integer
2016-09-17 14:45
204 查看
Reverse digits of an integer.
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.
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.
注意需要判断是否会溢出,若益处,则返回0
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.
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.
注意需要判断是否会溢出,若益处,则返回0
class Solution(object): def reverse(self, x): """ :type x: int :rtype: int """ num = 0 if abs(x) <= 9: return x abs_x = abs(x) while abs_x != 0: num = 10*num + abs_x % 10 abs_x = abs_x/10 if num > 0x7fffffff: return 0 else: return num if x > 0 else -num
相关文章推荐
- DB2中REVERSE函数的实现方法
- SQL Server中通过reverse取某个最后一次出现的符号后面的内容(字符串反转)
- javascript 数组排序函数sort和reverse使用介绍
- Javascript数组的排序 sort()方法和reverse()方法
- 在JavaScript中处理数组之reverse()方法的使用
- Javascript中数组sort和reverse用法分析
- js中reverse函数的用法详解
- Python 列表排序方法reverse、sort、sorted详解
- Windows下通过Putty和行程主机建立反向通道
- oracle reverse 反转函数
- 从Android访问PC端的port (reverse port forwarding)
- Lungo transition in reverse direction. 反转效果
- ReverseMap
- Reverse Words in a String
- 字符反转
- Like模糊查询与带有关键字Reverse的索引应用学习
- 黑马程序员_StringBuffer类的简介
- IDF 逆向题 python ByteCode
- 单向链表的逆置
- Reverse Integer