How to realize Integer inversion by LeetCode
Editor to share with you how to achieve integer inversion LeetCode, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
one
Topic description
Give a 32-bit signed integer, reverse the numbers on each of the integers and output them. For example, input 120 returns 21, input-12 returns-21. Only 32-bit signed integers are considered here. If the inverted number is not in [− 2 numbers 31, 2 bits 31 − 1], 0 is returned.
two
Python problem solution
Idea: the idea of converting an integer to a string is still very simple. First, judge the positive or negative of x. If it is an integer, convert it directly to a string and then output it in reverse order. If it is a negative number, you should pay attention to the position of the minus sign when converting it to a string. In addition, considering the range of values, the corresponding judgment should be made before output. Class Solution: def reverse (self, x: int)-> int: if x > = 0: y = int (str (x) [::-1]) else: y =-int (str (x) [: 0 str 1]) if 2 colors 31-1 > y >-2 cycles 31: return y else: return 0
The above is all the contents of the article "how to achieve integer inversion in LeetCode". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!