This is the fourth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

preface

Integer flipping is a very classic algorithm problem, in the daily work may not encounter such a business, but the idea of solving this problem is very important, there are many ways to solve, and I share with you the following method to solve this algorithm

Topic describes

Give you a 32-bit signed integer x and return the result of reversing the numeric portion of x.

If the reversed integer exceeds the range of 32 bit signed integers [−2^31, 2^31 − 1], 0 is returned.

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

Example 1:

Enter: x = 123

Output: 321

Example 2:

Enter: x = -123

Output: – 321

Example 3:

Enter: x = 120

Output: 21

Example 4:

Enter: x = 0

Output: 0

Their thinking

  • There are many ways to solve this problem, such as taking the input parameters and calculating the remainder by mathematical method, and then putting the remainder together backwards. However, I personally prefer to convert it into a string to solve the problem when dealing with this kind of problem
  • You can take the toString, turn it into a string, turn it into an array using the split method of the string, flip it over through a for loop, and then parseInt it into a number, and then handle the special cases, remember to handle the special cases (negative numbers and zeros, And if the input data is out of range)
  • Although a for loop can solve the problem, there is an easier way. In the previous step, we can use the reverse() method instead of the for loop, which greatly reduces the amount of code and looks more advanced at the same time

The code is as follows:

/ * * *@param {number} x
 * @return {number}* /
var reverse = function(x) {
    var sign= The '-'
      if(x>0){
        sign=0   
      }
        x = x.toString()
        x = x.split(' ').reverse()
        x = x.join(' ')
        x=parseInt(x)
        if(sign+x < -(2台湾国31) ||sign+x > 2台湾国31 -1) {return 0
        }
        else{
            return sign+x
        }
};

Copy the code

Note: Be sure to consider special cases, especially negative numbers and numbers that are out of range, otherwise submitting LeetCode will cause errors. If you’re thinking clearly, you can reduce the code to something like this:

/ * * *@param {number} x
 * @return {number}* /
 var reverse = function(x) {
    var sign= The '-'
      if(x>0){
        sign=0   
      }
        x = x.toString().split(' ').reverse().join(' ')
        x=parseInt(x)
        if(sign+x < -(2台湾国31) ||sign+x > 2台湾国31 -1) {return 0
        }
        else{
            return sign+x
        }
};
Copy the code

Does this look more comfortable? In LeetCode’s submit run results are also ok, as shown below:

conclusion

Numbers, arrays and strings in our life and work are exposed to a lot of, although this topic is very simple, but it is important to understand the truth, there must be a better, faster method, welcome everyone to give advice.