[LeetCode]7. Reverse Integer
来源:互联网 发布:沙箱软件 编辑:程序博客网 时间:2024/06/07 09:39
Description:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
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
Solution:
class Solution {public: int reverse(int x) { int result = 0; while (x != 0) { int temp = result * 10 + x % 10; // check if overflows. if ((temp - (x % 10)) / 10 != result) return 0; result = temp; x /= 10; } return result; }};
- [LeetCode]7.Reverse Integer
- LeetCode 7. Reverse Integer
- LeetCode --- 7. Reverse Integer
- LeetCode 7.Reverse Integer
- [Leetcode] 7. Reverse Integer
- [Leetcode]7.Reverse Integer
- LeetCode 7.Reverse integer
- [LeetCode] 7.Reverse Integer
- 7. Reverse Integer LeetCode
- leetcode 7. Reverse Integer
- leetcode 7. Reverse Integer
- leetcode 7. Reverse Integer
- Leetcode 7. Reverse Integer
- 【LeetCode】7.Reverse Integer
- LeetCode 7. Reverse Integer
- LeetCode *** 7. Reverse Integer
- LeetCode 7. Reverse Integer
- LeetCode-7. Reverse Integer
- 复杂的抽签问题
- Java8之工厂模式
- 无源汇有上下界可行流详解(loj115)
- java获取当前时间转换为时间戳 时间戳转换为时间实例
- Libevent-2.1.8源码分析——最小堆
- [LeetCode]7. Reverse Integer
- opencv仿射变换
- 欧几里得算法
- uva-1198
- oracle12c之 单机12.1.0.1打补丁
- Java 合并两个链表,得到一个新的链表
- IPC2
- R实战:【I/O】R画图中英文字体完美解决方案
- UVALive 7752 模拟