1 Integer inversion
Given a 32 – bit signed integer, you need to invert each digit of the integer. Note: Assuming that our environment can only store 32-bit signed integers, the value range is [-2^31, 2^31-1]. Based on this assumption, return 0 if the integer overflows after inversion.
- Example 1: Input: 123, output: 321
- Example 2: Input: -123, output: -321
- Example 3: Input: 120, output: 21
const reverse = x => { const edeg = 2**31; const max = edeg - 1; const min = -edeg; const result = (x > 0 ? 1 : -1) * String(x).split('').filter(x => x ! == '-').reverse().join(''); return result > max || result < min ? 0 : result };Copy the code