码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode: Reverse Integer

时间:2014-05-06 11:10:45      阅读:356      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

一次通过,它的spoiler里面的提示有两个:

1. 末尾为0的情况,这个考虑到了

2. 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).这种溢出的情况我没有考虑,不知怎么回事也通过了。下来有时间会想想这个问题。

bubuko.com,布布扣
 1 public class Solution {
 2     public int reverse(int x) {
 3         int res=0;
 4         int abs=Math.abs(x);
 5         int updatebit=0;
 6         while(abs/10!=0 || abs%10!=0){
 7             updatebit=abs%10;
 8             abs=abs/10;
 9             res=res*10+updatebit;
10         }
11         if(x<0){
12             res=(-1)*res;
13         }
14         return res;
15     }
16 }
bubuko.com,布布扣

 

Leetcode: Reverse Integer,布布扣,bubuko.com

Leetcode: Reverse Integer

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/EdwardLiu/p/3710643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!