Reverse Integer
来源:互联网 发布:瞻博网络中国区 编辑:程序博客网 时间:2024/04/27 22:24
Reverse Integer
Total Accepted: 3283 Total Submissions: 8003My SubmissionsReverse 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?
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).
class Solution {public: int reverse(int x) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int flag = (x > 0 ? 1 : -1); int result = 0; x = abs(x); while(x){ result = result*10 + x%10; x /= 10; } return flag * result; }};
- 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
- VC 显示图片自定义控件
- USACO ORZ
- 黑马程序员------------反射
- uva167 八皇后问题!输出固定宽度(#include<iomanip>)
- 山东理工大学ACM平台题答案 面向对象程序设计上机练习三(有默认参数的函数)1142
- Reverse Integer
- ${pageContext.request.contextPath} JSP取得绝对路径
- 黑马程序员----jdk1.5新特性
- STM32跳至硬件错误中断(HardFault_Handle)
- Windows窗口风格详细解释
- 断点续传,ibm那里拿来的文章,真心好,学习下
- 数据库缓存依赖命令 aspnet_regsql
- 多线程,socket,HTTP,ASIHttpRequest,等面试题总结集合
- 深究引用类型变量