Reverse Integer
2015-08-31 23:36
531 查看
Reverse Integer
Total Accepted: 92759 TotalSubmissions: 391368My Submissions
Question
Solution
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
#include<iostream> #include<list> using namespace std; class Solution { public: int reverse(int x) { const int MAX_1 = 2147483647; const int MAX_2 = -2147483648; if( x == MAX_2 || x == 0) return 0; bool IsNegative = false; if( x < 0) { IsNegative = true; x = -x; } list<char> num; while(x) { char tmp = x % 10; num.push_back( tmp ); x = x / 10; } long long l_re = 0; long long mi = 1; while( num.size()){ l_re += mi * num.back(); num.pop_back(); mi = mi * 10; } if( l_re > MAX_1) return 0; else return IsNegative ? -l_re : l_re; } }; int main() { Solution solution; cout<<solution.reverse(1534236469); getchar(); return 0; }
相关文章推荐
- C/C++编程细节(二)——堆栈、数组、指针
- 读写XML
- 剑指Offer面试题:18.二叉树的镜像
- 弹性盒模型
- hdu 5425 Rikka with Tree II(暴力)
- 深夜的一些想法
- ajax的再回首
- 【解决】eclipse不显示Android SDK Manager标签
- 菜鸟教程之工具使用(二十)——解决m2eclipse插件无法执行Maven命令
- hive的安装
- VI编辑器使用总结
- java实现冒泡排序
- Tomcat配置二
- Java多线程之线程分类【案例分析】
- 设计模式之建造者模式---Builder
- .Net自帶Ajax和GridView
- 微软 自带 AJAX 拓展
- Linux命令备忘实例(1)——终端输出
- 练级攻略
- hdu 5424 Rikka with Graph II(欧拉通路)