Reverse Integer
2015-10-03 07:42
239 查看
题目:Reverse digits of an integer.
Example1: x= 123, return 321
Example2: x =
-123, return -321
思路:迭代计算,保存末尾值。注意一开始的负号
第一个需要注意的就是判断最大值最小值,尤其是最小值,这一点在后面的求解除法中非常明显。代码:
class Solution {public:<span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);">//for more information,please email:j.z.feng@foxmail.com</span><span style="margin: 0px; padding: 0px; border: none; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);"> </span>
//https://leetcode.com/problems/reverse-integer/
int reverse(int x) {
if(x<0){
return x==INT_MIN ? 0 : -reverse(-x);
}
int y,result=0;
//x=32767
if(x==0){
return 0;
}
while(x>0){
if(result>INT_MAX/10){
return 0;
}
if(result==INT_MAX/10 && (x%10)>7){
return 0;
}
result=result*10+(x%10);
x=x/10;
}
return result;
}
};
相关文章推荐
- 学习方法总结
- 1.2.2 加载第二部分代码—setup(1)
- Pascal's Triangle II
- ViewPager不显示加载的图片或者报错
- iOS—修改AFNetworking源文件可接收text/html的方法
- 1.2.1 加载第一部分代码—引导程序(bootsect)
- PS 利用置换滤镜制作动感水波字
- 1.1.2 BIOS在内存中加载中断向量表和中断服务程序
- PS 水波效果
- DataGridView的Cell事件的先后触发顺序
- 1.1.1 BIOS的启动原理
- 水波的模拟
- 1.1 启动BIOS,准备实模式下的中断向量表和中断服务程序
- 相比薪酬,学习效率提升才是创业公司最有价值的报酬
- Leetcode NO.223 Rectangle Area
- 目录
- Mysql命令大全
- 解释水波特效处理
- OSChina 周六乱弹—— 小小编摊上大事儿啦
- 水波模拟算法