Reverse Integer

Analysis:
Need to consider overflow. It's easy to use long instead of int when handling overflow issue.
Solution:


 class Solution {  
   public int reverse(int x) {  
     long res = 0;  
     while (x!=0) {  
       res *= 10;  
       res += x%10;  
       x /= 10;  
       if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE)  
         return 0;  
     }  
     return (int)res;  
   }  
 }   

评论

此博客中的热门博文

776. Split BST

663. Equal Tree Partition

532. K-diff Pairs in an Array