Solution in C++ by foroot


  • 0
    F
    class Solution {
    public:
    	int reverse(int x) {
    		long long sign = x >= 0 ? 1 : -1;
    		long long ret = 0, val = x * sign; \\ make x as a positive, could overflow if x = x * sign when x == INT_MIN
    
    		while (val)
    		{
    			ret = ret * 10 + val % 10;
    			val /= 10;
    		}
    		ret *= sign;
    		return (int)((ret < INT_MIN || ret > INT_MAX) ? 0 : ret);
    	}
    };
    

Log in to reply
 

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