/** * @param {number} x * @return {number} */ var reverse = function(x) { let max = Math.pow(2,31) - 1 let min = Math.pow(-2,31) let result = 0, pop while(x !== 0){ pop = x % 10 x = (x - pop) / 10 result = result * 10 + pop } if(result > max || result < min) return 0 return result }
利用整数取模反转,注意判断溢出情况。