Solution in Java


  • 0
    M
    class Solution {
        public int reverse(int x) {
             long rev = 0;
    		 int flg = 0;
    		 if(x < 0) {
    			 flg = 1;
    			 x = x*-1;
    		 }
    		 while(x >= 1) {
    			 
    			 int lastd = x%10;
    			 rev = rev*10 + lastd;
    			 x = x/10;
    			 
    		 }
             if(rev < Integer.MIN_VALUE || rev > Integer.MAX_VALUE) {
    			 return 0;
    		 }
    		 if(flg == 1) {
    			 
    			 return (int)rev * -1;
    		 }
             if(rev < Math.pow(-2, 31) && rev > Math.pow(2, 31) ) {
    			 return 0;
    		 }
    		return (int)rev;
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.