LeetCode 007: Reverse Integer
来源:互联网 发布:opencv骨架提取算法 编辑:程序博客网 时间:2024/05/18 02:16
007. Reverse Integer
Difficulty: Easy
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.
思路
每次x对10取余,余数乘10累加起来。
要注意的是,返回值必须是int类型,反转后的数有溢出的可能,处理时使用范围比int大的整型类型,判断数值是否超出int的范围。
代码
[C++]
class Solution {public: int reverse(int x) { long res = 0; while (x) { res = res * 10 + x % 10; x /= 10; } cout << res; if (res < INT_MIN || res > INT_MAX) return 0; return (int)res; }};
- LeetCode 007 Reverse Integer
- LeetCode 007 Reverse Integer
- [LeetCode]-007-Reverse Integer
- LeetCode 007 Reverse Integer
- 007 Reverse Integer [Leetcode]
- [LeetCode]007-Reverse Integer
- LeetCode 007 Reverse Integer
- leetcode Reverse Integer 007
- leetcode 007 Reverse Integer
- LeetCode 007 Reverse Integer
- LeetCode 007: Reverse Integer
- leetcode-007-Reverse Integer
- leetcode 007 Reverse Integer
- LeetCode-007 Reverse Integer
- [LeetCode OJ][007]Reverse Integer
- 【LeetCode OJ 007】Reverse Integer
- leetcode 007 —— Reverse Integer
- leetcode之路007 Reverse Integer
- InputStream和OutputStream的比较
- Winform 利用ThoughtWorks.QRCode 生成二维码,并另存为二维码图片(限制大小)
- 深入理解Android的startservice和bindservice
- python 函数
- oralce 产生随机数
- LeetCode 007: Reverse Integer
- ORA-00984列在此处不允许
- 贝叶斯估计
- C的|、||、&、&&、异或、~、!运算符
- Linux下重命名文件或文件夹(mv命令与rename命令)
- JSP 中 pageEncoding charset 的区别
- Coursera机器学习-第四周-Neural Network ForwardPropagation
- Appium的DesiredCapabilities参数设置
- 给按钮设置按下抬起状态