Solution in C++


  • 0
    N
            int Reverse(int x) {
    		if (x == INT_MIN || x == 0)
    			return 0;
    
    		int sign = x < 0 ? -1 : 1;
    		int val = abs(x);
    
    		int ret_int = 0;
    		while (val > 0) {
    			if (INT_MAX / 10 < ret_int)
    				return 0;
    			ret_int = ret_int * 10 + val % 10;
    			val /= 10;
    		}
    
    		return ret_int * sign;
          }
    

Log in to reply
 

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