[Leetcode] [simple] [17] [JavaScript] integer reversal

Title Description

7. integer reversal

Gives a 32-bit signed integer, you need this integer number on each inverted.

Example 1:

Input: 123
Output: 321
 Example 2:

Input: -123
Output: -321
Example 3:

Input: 120
Output: 21
Note:

Suppose we have an environment can only store a 32-bit signed integer, its value is in the range [-2 31 is 2 31 is  - 1] Please According to this hypothesis, if integer overflow after reverse it returns 0.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/reverse-integer
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

 

answer

var reverse = function(x) {
    
    if(typeof(x)==='number'){

        let str = ''
        let result = null
        
        str = x.toString().split('').reverse().join('')
        
        if(x<0){
           str = '-' + str
           }
        
        result = parseInt(str)

        if(result>2147483648 || result <-2147483648){
           return 0
           }
           return result
    }
};

 

 

 

 

Guess you like

Origin www.cnblogs.com/2463901520-sunda/p/11441173.html