7. Reverse Integer(js)
7. Reverse Integer
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
题意:将int型的整数翻转输出
代码如下(js):
var reverse = function(x) { var res=0; while(x!=0){ //取余 res=res*10 + x%10; //减余除10 x=(x-x%10)/10; // console.log(res,x) } //判断是否越界 if(Math.pow(2,31)<res || Math.pow(2,31)<-res) return 0; // console.log(res); return res; };