整数反转
Example1: x = 123, return 321
Example2: x = -123, return -321
@param {number} x
@return {number}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| var reverse = function(x) { var temp; var first = ''; if(x < 1e+32){ temp = x.toString(); if(temp[0] === '-'){ first = '-'; temp = temp.substr(1); } temp = temp.split(''); temp.reverse(); temp = temp.join(''); temp = first + temp; return Number(temp); }else { return 0; } };
var reverse = function(x){ var res = 0; if(x < 1e+32){ while(x > 1 || x < -1){ res = res * 10 + parseInt(x % 10); x /= 10; } return res; }else { return 0; } }
console.log(reverse(123));
|