Realization method of integer inversion in python3

  • 2021-10-16 02:14:10
  • OfStack

Give you a 32-bit signed integer x, and return the result of partial inversion of the numbers in x.

Returns 0 if the inverted integer exceeds the range of 32-bit signed integers [2 ^ 31, 2 ^ 31-1].

Assume that the environment does not allow 64-bit integers (signed or unsigned) to be stored.

Example 1:

Input: x = 123
Output: 321

Example 2:

Input: x =-123
Output:-321

Example 3:

Input: x = 120
Output: 21

Example 4:

Input: x = 0
Output: 0

Idea 1: Turn it into a string for flipping, and judge the positive and negative. Finally, the question requires that 0 be returned if the inverted integer exceeds the range of 32-bit signed integers [2 ^ 31, 2 ^ 31-1]


class Solution:
  def reverse(self, x: int) -> int:
    str1 = str(x)
    
    if str1[0] == '-':
      str1 = str1[0] + str1[:0:-1]
    else:
      str1 = str1[::-1]
    return int(str1) if -2147483648<int(str1)<2147483648 else 0

Idea 2: Do not use strings. Returns 0 when the flipped number is greater than the condition


class Solution:
  def reverse(self, x: int) -> int:
 y, res = abs(x), 0
    #  The value range is  [−2^31, 2^31 − 1]
    boundry = (1<<31) -1 if x>0 else 1<<31
    while y != 0:
      res = res*10 +y%10
      if res > boundry :
        return 0
      y //=10
    return res if x >0 else -res

Improvement:


class Solution:
  def reverse(self, x: int) -> int:
    str1 = str(x)
    
    if str1[0] == '-':
      str1 = str1[0] + str1[:0:-1]
      a=int(str1)
      if (1<<31)<abs(a):
        return 0
    else:
      str1 = str1[::-1]
      a= int(str1)
      if a>(1<<31) -1:
        return 0
    return a 

Add: Q: How many methods do Python reverse 3-bit integers?

A: This is a programming algorithm on leetcode. It feels quite classic. Today, I will take it out and share it with you! Given a positive 3-digit integer, you need to reverse the number on each person in this integer. For example: Input: 123, Output: 321. Let's not look at the following answers first, and see if we are ourselves, we can come up with several Python ways to solve them!

Let's talk about several ways to achieve it:

1. Rounding method


class Test:
  def reverseInteger(self, number):
    g = number%10     # Take out single digits 
    s = (int(number/10))%10  # Take out 10 Number of digits 
    b = int(number/100) # Take out hundreds of digits 
    return g*100+s*10+b
if __name__ == "__main__":
  ts = Test()
  print (ts.reverseInteger(123)) # Output: 321

2. The way to use the stack


class Test:
  def reverseInteger(self, number):
    number = str(number) #  Analog stacking 
    l = list(number)
    result = ""
    while len(l) > 0:
      result += l.pop() #  Analog stack 
    return int(result)
if __name__ == "__main__":
  ts = Test()
  print (ts.reverseInteger(123)) #  Output: 321

3. Use slicing


class Test:
  def reverseInteger(self, number):
    number=str(number)
    result=number[::-1] #python Special usage of slices in 
    result=(int(result))
    return result
if __name__ == "__main__":
  ts = Test()
  print (ts.reverseInteger(123))


4. Integer to string, invert string, and then turn integer


class Test:
  def reverseInteger(self, x):
    plus_minus = ""
    reverse_x = ""
    if x < 0:
      plus_minus = "-"
      x = -x
    for i in str(x):
      reverse_x = i + reverse_x
    reverse_x = plus_minus + reverse_x
    if int(reverse_x) > pow(2, 31) - 1 or int(reverse_x) < pow(-2, 31):
      return 0
    return int(reverse_x)
 
if __name__ == "__main__":
  ts = Test()
  print (ts.reverseInteger(123)) # Output: 321

Related articles: