Reverse Integer
来源:互联网 发布:wi.cap网络嗅探汉化版 编辑:程序博客网 时间:2024/06/18 17:46
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
纯数学
public class Solution { public int reverse(int x) { int x1=x<0?0-x:x; int result=0; while(x1!=0) { result=result*10+x1%10; x1=x1/10; } return x<0?0-result:result; }}
以下特殊情况还没有考虑 用exception做
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).
0 0
- 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
- 黑马程序员_方法、参数、返回值及冒泡排序
- Same Tree
- js 大小写 小括号大括号 定义函数 调用函数 递归!!!
- 调整数组顺序使奇数位于偶数前面
- 黑马程序员_类的学习
- Reverse Integer
- [AppArmor-GUI]二、AppArmor学习与认识之一
- hoj 1687 经理的烦恼
- 腾讯er面
- 黑马程序员_winform和wpf学习
- C++学习之函数模板与类模板
- 3Sum
- 黑马程序员_音乐(视频)播放器学习
- 黑马程序员_基础html css 学习(一)