LeetCode-Reverse Integer
来源:互联网 发布:淘宝借贷有保证吗 编辑:程序博客网 时间:2024/06/13 16:59
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
Solution: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?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
Code:
<span style="font-size:14px;">class Solution {public: int reverse(int x) { int pos = x>0?1:-1; x *= pos; int result = 0; while (x > 0) { result = result*10+x%10; x /= 10; } return pos*result; }};</span>
0 0
- LeetCode:Reverse Integer
- LeetCode: Reverse Integer
- [Leetcode] Reverse Integer
- LeetCode : Reverse Integer
- [LeetCode] Reverse Integer
- Leetcode: Reverse Integer
- LeetCode 7 - Reverse Integer
- leetcode 104: Reverse Integer
- Leetcode : Reverse Integer
- 【leetcode】Reverse Integer
- [LeetCode]Reverse Integer
- LeetCode-Reverse Integer
- [leetcode] Reverse Integer
- LeetCode - Reverse Integer
- leetcode之Reverse Integer
- LeetCode | Reverse Integer
- LeetCode: Reverse Integer
- Leetcode: Reverse Integer
- 功率计远程控制
- 启动tomcat这个问题怎么解决????
- hdu 4336 期望 状压dp
- ZVB网分仪远程控制
- QList介绍
- LeetCode-Reverse Integer
- qt 和 tslib移植
- Win7+C++开发环境搭建
- 啸叫抑制装置
- CSS样式设置与框模型
- Hibernate报错:SQLGrammerException:Could not execute native bulk manipluation query(执行完sql报错)
- DM365+live555实现RTSP直播服务器(广播)
- eclipse 真机调试整理!
- 打开MAC OS X系统自带读写NTFS功能