LeetCode--Reverse Integer
2015-07-04 17:47
495 查看
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
问题描述: 将整数个十百位反序输出。
注意特殊情况:
1)溢出情况:To check for overflow/underflow, we could check if ret > 214748364 or ret < –214748364 before multiplying by 10. On the other hand, we do not need to check if ret == 214748364, why?
2)正数、负数除10 余10的情况;
3)10,100这样的数反序时可不可以用一般的程序处理。
代码:
Example1: x = 123, return 321
Example2: x = -123, return -321
问题描述: 将整数个十百位反序输出。
注意特殊情况:
1)溢出情况:To check for overflow/underflow, we could check if ret > 214748364 or ret < –214748364 before multiplying by 10. On the other hand, we do not need to check if ret == 214748364, why?
2)正数、负数除10 余10的情况;
3)10,100这样的数反序时可不可以用一般的程序处理。
代码:
public class Solution { public int reverse(int x) { int max = Integer.MAX_VALUE; int min = Integer.MIN_VALUE; long res = 0; while(x!=0){ int n = x%10; res = res *10 + n; x = x/10; if(res>max || res<min) return 0; } int res1 = (int) res; return res1; } }
相关文章推荐
- Mac下的hadoop环境搭建(单节点)
- Unity-EasyTouch插件之Two Finger
- Unity-EasyTouch插件之Two Finger
- error C2039: ‘ReadHuge’ : is not a member of ‘CFile
- C#: 根据指定压缩比率压缩图片
- UOJ#35 后缀排序
- 实习第2天
- guava学习
- 黑马程序员——Java基础---线程的另一个总结(5)--线程池,线程定时器
- C#: 缩放图片
- 1039. Course List for Student (25)
- Bringing up interface eth2: Error: No suitable device found: no device found for connection 'System
- 0长度数组
- makefile与configure
- 1039. Course List for Student (25)
- C#: 旋转图片到正确位置
- error C2668: ‘pow’ : ambiguous call to overloaded function
- 获取微信昵称、头像等信息方法
- MongoDB的学习--explain()和hint()
- iPhone 6s逻辑板曝光,居然还是16GB