Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21Note:Assume we are dea...
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123 Output: 321
Example 2:
Input: -123 Output: -321
Example 3:
Input: 120 Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
意思就是把整数反转过来,代码如下:
var reverse = function(x) {
var number='';
var arr=''+x;
var numArr=arr.split("");
numArr.reverse();
if(numArr[numArr.length-1]=='-'){
number+='-';
numArr.splice(numArr.length-1,1);
}
for(var i=0;i<numArr.length;i++){
number+=parseInt(numArr[i]);
}
number=parseInt(number);
if(number>Math.pow(2,31)-1||number<(-Math.pow(2,31))){
number=0;
}
return number;
};
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)