Today, I’m going to share with you a little bit of knowledge — flipping an integer

Believe me, so so so easy… And it’s easy to understand.

Well, let’s not talk more about gossip and get straight to today’s main topic.

So first let’s see what the problem is.

Implement the flip of an integer

Such as:

  1. Initial value: 123; flipped value: 321
  2. Initial value: -123; flipped value: -321

First don’t look at the answer, according to their own ideas to answer again.


1. The first edition

How many of you, when you look at the problem, immediately think of the following solution,

  • First, the number is converted to a string
  • Then judge the first character
    • If it is a symbol, the content after the first character is truncated
    • If it is not a symbol, truncate all
  • The resulting string is then appliedstr.split('')operation
  • And then I’m going to take the array that I getreverse
  • Then decide whether to add the symbol prefix
  • And then you get the right answer.

🌰 chestnuts:

function reverseInt (n) {
  let str = `${n}`.split(' ')
  let firstChar = str[0]

  if (firstChar === The '-') {
    str = str.slice(1)}else {
    firstChar = ' '
  }
  str = str.reverse().join(' ')
  return (firstChar + str) * 1
}
Copy the code

Now let’s analyze the advantages and disadvantages of this solution

Advantage:
  • Easy to understand, we can clearly know how each step is executed (seems to have only one advantage)
Disadvantage:
  • Waste of resources
    • You need to convert numbers to strings first
    • And then it splits into arrays
    • And then flip the array
    • And then it’s converted to a string
    • Finally converted to a number.
  • You also need to pay attention to whether the current integer is negative or positive.

Of course, this solution must be correct, this is indisputable, and everyone has everyone’s understanding.

But ~ ~

There are still buts, but when you write code, you have to be a little bit more ambitious, you have to be a little bit more effortless to solve a problem.

Now let’s look at another solution

2. Second edition

The version that follows is the mathematical version, but it’s also the easier one to understand.

Well, without further ado, let’s go straight to chestnut!

🌰 chestnuts:

function reverseInt (n) {
  let result = 0

  while(n) {
    let b = n % 10
    n = ~~(n / 10)
    result = result * 10 + b
  }
  return result
}
Copy the code

This may seem a little confusing at first, but don’t panic, let’s explain how we did it.

Explanation:
  • First, we define a result variableresultTo save the results of our run.
  • Loop variablesn, known0Did not end.
  • And then there’s the big story, the core implementation of this algorithm.
    • Every time we loop, we’re going to changenThe ones digit value of is saved.
    • And then let thenThe length of the cut1
    • Then let the result variable add the value we saved ten times each time.
  • After the loop is overresultVariable returns.
  • Here we use an operator like this~ ~This is a bit operation. It means round the result of the operation. (Check out my previous post for more details.)
Process details:

Let’s take a look at the algorithm’s execution steps using the original value 123.

First loop:

n = 123

result = 0

b = 123 % 10 = 3

N = 123/10 = 12

result = 0 * 10 + 3 = 3

Second loop:

n = 12

result = 3

b = 12 % 10 = 2

N is equal to 12/10 is equal to 1

result = 3 * 10 + 2 = 32

Third loop:

n = 1

result = 32

b = 1 % 10 = 1

N = 1/10 is equal to 0

result = 32 * 10 + 1 = 321

At this point the whole algorithm is done. The problem with this algorithm is that it also works for negative numbers.

Now let’s look at the advantages of this algorithm

Advantage:
  • You don’t have to worry about negative numbers.
  • Saves a lot of conversion costs.
  • Save space
  • Mathematical calculations are used to avoid the time consumption of manipulating strings and arrays.

OK, that’s the end of our sharing for today.

Also, expect to bring you a different understanding and perception.

Bye~