7. Reverse Integer *
来源:互联网 发布:java后端是做什么的 编辑:程序博客网 时间:2024/05/18 18:17
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
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.
My code:
class Solution(object): def reverse(self, x): """ :type x: int :rtype: int """ result = 0 n = abs(x) intMax = 2**31-1 while n>0: if intMax /10 <result or intMax - n%10 <result*10: return 0 result =result*10 + n%10 n /= 10 if x <0: return result*(-1) return result
Note: Think about the overflow problem
Reference
0 0
- [LeetCode]7.Reverse Integer
- LeetCode 7. Reverse Integer
- LeetCode --- 7. Reverse Integer
- LeetCode 7.Reverse Integer
- [Leetcode] 7. Reverse Integer
- 7.Reverse Integer
- [Leetcode]7.Reverse Integer
- LeetCode 7.Reverse integer
- 7. Reverse Integer
- 7. Reverse Integer
- 7.Reverse Integer
- 7.Reverse Integer
- [LeetCode] 7.Reverse Integer
- 7. Reverse Integer
- 7. Reverse Integer
- 7. Reverse Integer
- 7. Reverse Integer
- 7. Reverse Integer LeetCode
- 详解CoordinatorLayout
- 1.Two Sum 难度:easy
- 基本的web页面样式(就是一开始把所有的基础样式都规定好)
- cocos2d-x--->飞机大战第一天0905
- 数组
- 7. Reverse Integer *
- 怎么判断一个数是否为完全平方数
- 欢迎使用CSDN-markdown编辑器
- cocos2d-x视频教程
- KNN、k-近邻算法,python
- C#中ref和out到底有什么用?什么时候会用到呢?
- CodeForces 617B2
- poj 1990
- 【NOI2014】魔法森林