Reverse Integer
来源:互联网 发布:数据分析作图软件 编辑:程序博客网 时间:2024/06/04 21:37
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.
Update (2014-11-10):
Test cases had been added to test the overflow behavior.
class Solution {public: int reverse(int x) { int sgn = x>=0?1:-1; long long res =0; x = sgn*x; while(x>0){ res = res*10+x%10; if(res>INT_MAX) return 0; x = x/10; } return sgn*res; }};
0 0
- Reverse Integer
- Reverse Integer
- Reverse Integer
- reverse integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- reverse Integer
- Reverse Integer
- Reverse integer
- Reverse Integer
- Reverse Integer
- SQL DDL语句
- CentOS 7 版本如何实现开机以图形界面启动
- Android 各大网络请求库的比较及实战
- hdu1272 小希的迷宫 并查集
- 常用软件安装与配置小结
- Reverse Integer
- spring自带定时器
- spark:学习杂记--38
- linux后台运行和关闭、查看后台任务
- 作业5.19
- 第三章P123作业T29,30,32,36,38
- 读取Assets中的文件数据
- java动态代理(JDK和cglib)
- FZU 星系碰撞 最大独立集 匈牙利算法