Solution in Javascript


  • 0
    H
    const maxNum = Math.pow(2, 31) - 1;
    
    /**
     * @param {number} n
     * @return {number}
     */
    function reverse(n) {
    	const isNeg = n < 0 ? -1 : 1;
    	let newN = 0;
    
    	// Reverse negativity
    	n *= isNeg;
    
    	while (n > 0) {
    		newN = newN * 10 + n % 10;
    		n = Math.floor(n / 10);
    	}
    
    	// Return new number with negativity re-applied
    	return (newN > maxNum) ? 0 : newN * isNeg;
    };
    

Log in to reply
 

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