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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 dnf账号改错名了怎么办 银行卡绑定的手机号码换了怎么办 支付宝手机号码换了怎么办 淘宝账号被注销了怎么办 注销淘宝号绑定的手机号怎么办 淘宝账号不小心注销了怎么办 淘宝旧密码忘了怎么办 淘宝登录原始密码忘记了怎么办 微信原始密码忘记了怎么办 优酷会员重复交费怎么办 微信解绑手机号密码忘了怎么办 闪银呼呼逾期5天怎么办 忘记淘宝账号和密码怎么办 蘑菇街账号忘了怎么办 台式电脑密码忘记了怎么办 单位社保登陆密码忘记了怎么办 12306的登录密码忘了怎么办 网银支付密码忘了怎么办 邮政网银密码忘了怎么办 12306新注册待核验怎么办 建行网银盾密码忘了怎么办 建行网银登陆密码忘了怎么办 建行网银密码忘了怎么办 建行手机网银密码忘了怎么办 移动宽带账号密码忘了怎么办 移动宽带忘记账号密码怎么办 宽带账号密码忘了怎么办 不知道宽带账号密码怎么办 宽带的账号密码忘记了怎么办 wifi登录名忘记了怎么办 苹果手机微信图纸打不开怎么办 手机qq邮箱文件打不开怎么办 12360忘记用户名和密码怎么办 刚开店铺没生意怎么办 淘宝账户不符合注销条件怎么办 网易邮箱登录密码忘记了怎么办 q号密码忘记了怎么办 志愿者注册忘记密码和用户名怎么办 w10电脑语言栏不见了怎么办 w10美式键盘没了怎么办 xp电脑开机密码忘记了怎么办