leetcode:Reverse digits of an integer
来源:互联网 发布:amd优化 编辑:程序博客网 时间:2024/06/04 06:14
//Reverse digits of an integer.//Example1: x = 123, return 321//Example2: x = -123, return -321//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).using namespace std;#include "Solution12.h"//#include <stack>Solution12::Solution12(){}Solution12::~Solution12(){}//方法1://int Solution12::reverse(int x)//{//if (x<0)//{//x=abs(x);//}//int max_int=0x7fffffff;//int min_int=0x80000000;//int sum=0;//while (x!=0)//{//int temp=x%10;//sum=sum*10+temp;//if (sum > max_int || sum< min_int)//{//sum=sum>max_int?max_int:max_int;//return sum;//}//x=x/10;//}//return sum;//}//方法二:class Solution { public: int reverse(int x) { int sign = x < 0? -1: 1; int ret = 0; x = abs(x); while(x > 0) { ret = ret*10 + x%10; x /= 10; } return sign*ret; } };两种方法都AC,道理差不多。
0 0
- leetcode 75: Reverse digits of an integer.
- 【LeetCode】Reverse digits of an integer
- leetcode:Reverse digits of an integer
- LeetCode Reverse digits of an integer.
- leetcode Reverse digits of an integer
- leetcode 7 Reverse digits of an integer
- leetcode-T7 Reverse digits of an integer
- LeetCode 7.Reverse digits of an integer.
- Reverse digits of an integer.
- reverse digits of an integer.
- Reverse digits of an integer.
- Reverse digits of an integer.
- Reverse digits of an integer.
- reverse digits of an integer
- Reverse digits of an integer.
- Problem 7.Reverse digits of an integer.
- Reverse digits of an integer(数字倒换)
- 【C++】Reverse digits of an integer
- Java中的Timer和TimerTask的使用
- C/C++调试基础
- Web服务器父与子 Apache和Tomcat区别
- 如何让maven 将工程依赖的jar 复制到WEB-INF/lib 目录下?
- js 简单学习笔记
- leetcode:Reverse digits of an integer
- An Introduction to Handlebars(Handlebars 简介)
- 有些事我们被骗了快20年
- Random获取随机数
- Ubuntu13.10下禁用N卡独显方法
- selinux 禁用后(disabled)Linux系统无法正常启动的问题
- 10种构建J2EE方法让架构设计师快速成为专家
- maven常见问题问答
- jquery学习笔记