Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

 

public class Solution {
    public int reverse(int x) {
          int v= Math.abs(x);
       if(v==0)return 0;
       int r=0;
       int previous=0;
       while(v>0){
           previous=r;
           r=r*10+v%10;
           if(r/10!=previous)return 0;
           v=v/10;
       }
      
      
       return x>0?r:-r;
    }
}

 

posted @ 2015-10-04 00:41  stonehat  阅读(157)  评论(0编辑  收藏  举报