Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
var min_32 = 1<<31; var max_32 = parseInt(‘7fffffff‘,16); var reverse = function(x) { var syn=0; if(x < 0){ x = Math.abs(x); syn=1; } x = x + ""; var len = x.length; var arr = x.split(‘‘); for(var i=0; i<len/2; i++){ var temp = x[i]; arr.splice(i,1,x[len-i-1]); arr.splice(len-i-1,1,temp); } x = parseInt(arr.join("")); if(x> max_32) { return 0; } if(x< min_32){ return 0; } if(!syn) return x; else return -x; };
Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
var isPalindrome = function(x) { if(x<0) return false; var a = []; var n = 10; while(x>0){ a.push(x%10); x = parseInt(x / 10); } var len = a.length; for(var i=0;i<len/2;i++){ if(a[i] != a[len-1-i]) return false; } return true; };
时间: 2024-10-10 10:52:19