Make writing a habit together! This is my first day to participate in the “Gold Digging Day New Plan · April More text challenge”, click to see the details of the activity.

I. Title Description:

You are given a 32-bit signed integer x that returns the result of reversing the numeric portion of x.

If the inverted integer exceeds the range of 32-bit signed integers [−231, 231 − 1], 0 is returned.

Assume that the environment does not allow storage of 64-bit integers (signed or unsigned).

Example:

The sample1: Enter x =123Output:321The sample2: Enter x = -123Output: -321The sample3: Enter x =120Output:21The sample4: Enter x =0Output:0
Copy the code

Tip:

  • -231 <= x <= 231 - 1

Ii. Solution:

You have to think about how to reverse the numbers.

There are three simple methods: array-to-tail push, string to tail concatenation, and reverse.

There are three pitfalls to this problem

- It is within a certain range before flipping, and there is a possibility, namely, the problem of out of bounds after flipping - the last bit or several bits are 0 before flipping, and need to be removed after flipping - the problem of coincidence before and after flippingCopy the code

Method 1: string concatenation

  • Principle. According to the solution traversal into the appropriate number splicing can be.
  • Train of thought.
    • If it is negative, first extract the sign bit and then normalize the number (take the absolute value).
    • traverse
    • Take the last bit by taking the remainder
    • Concatenates strings and skips cases where the last or more bits are 0
    • Use division + integer operation to change the number traversed
    • Then judge the boundary problem

Code:

var reverse = function(x) {
    // if (0) = 0
    if(x===0) {return 0
    }
    
    let resStr = ' '
    // If the value is negative, the symbol bit should be extracted first and then the number should be normalized
    if(x<0){
        resStr+=The '-'
        x = Math.abs(x)
    }
    // If x is not 0
    while(x){
        // Retrieve the last bit with the remainder
        let item = x%10
        // If resStr is empty and the mod extract is 0, nothing is done and the last or several bits are 0 are skipped
        if(resStr.length || item! = =0) {
            resStr+=item
        }
        // Use the division + round operation to reduce the length of x, lose the last bit
        x = parseInt(x/10)
    }
    resStr = Number(resStr)
    let bigNum = 2**31
    // The boundary problem after the inversion
    if(resStr>bigNum-1 || resStr<-1*bigNum){
        return 0
    }
    return resStr
}
Copy the code

It can also be replaced with an array operation:

var reverse = function(x) {
    // if (0) = 0
    if(x===0) {return 0
    }
    
    let resArr = []
    // If the value is negative, the symbol bit should be extracted first and then the number should be normalized
    if(x<0){
        resArr.push(The '-')
        x = Math.abs(x)
    }
    // If x is not 0
    while(x){
        // Retrieve the last bit with the remainder
        let item = x%10
        // If resArr is null, and the number of bits to be extracted is 0, then nothing is done and the last or several bits are 0 is skipped
        if(resArr.length || item! = =0) {
            resArr.push(item)
        }
        // Use the division + round operation to reduce the length of x, lose the last bit
        x = parseInt(x/10)
    }
    resArr = Number(resArr.join(' '))
    let bigNum = 2**31
    // The boundary problem after the inversion
    if(resArr>bigNum-1 || resArr<-1*bigNum){
        return 0
    }
    return resArr
}
Copy the code

Method two uses the array method

  • Principle. Using arrays according to the problemreverseFlip the digital splice.
  • Train of thought.
    • If it is negative, first extract the sign bit and then normalize the number (take the absolute value).
    • traverse
    • Concatenate the reversed number
    • Then judge the boundary problem
  • Pay attention to.This method converts the number to a string and then splits it into an array using split

Code:

var reverse = function(x) {
    if(x===0) {return 0
    }
    let resStr = ' '
    if(x<0){
        resStr+=The '-'
        x = Math.abs(x)
    }
    resStr += String(x).split(' ').reverse().join(' ')
    resStr = Number(resStr)
    let bigNum = Math.pow(2.31)
    if(resStr>bigNum-1 || resStr<-1*bigNum){
        return 0
    }
    return resStr
};
Copy the code

Third, summary

  • This problem can be string concatenation method and clever array method of two schemes
  • String splicing method is mainly according to the solution of the problem traversal into the appropriate number splicing.
  • Use arraysIt’s basically using arrays according to the problem solvingreverseFlip the digital splice.

If there are any errors in this article, please correct them in the comments section