当前位置:网站首页>Leetcode 7. integer inversion
Leetcode 7. integer inversion
2022-07-29 06:22:00 【Zhangchuming ZCM】
Power button | 7. Integer inversion
Title screenshot

Method 1 : Mathematical method
Ideas :
utilize “%” modulus , utilize “/” Divisor .
Be careful :python The default is floating point algorithm , So add int() Cast to integer .
At the same time, the topic requires that the input and output range must be
, Make two judgments here .
At the same time, it is necessary to judge whether it is a negative number .
Input x by 0 Time is consistent with positive numbers , Don't make a distinction .
I divide it into two methods . When the input is negative , First turn negative numbers into positive numbers , Then the method of directly referring to positive numbers is reversed , Finally, change it to a negative number .
class Solution:
def reverse(self, x: int) -> int:
if -2 ** 31 <= x <= 2 ** 31 - 1:
if x < 0:
x = -x
y = (-1) * self.reverse_Positive_number(x)
else:
y = self.reverse_Positive_number(x)
else:
y = 0
if -2 ** 31 <= y <= 2 ** 31 - 1:
pass
else:
y = 0
return y
def reverse_Positive_number(self, x: int) -> int:
y = 0
while x > 0:
temp = x % 10
x = int(x / 10)
y = y * 10 + temp
return yCode optimization :
yes int Value range of type ,python There is no INT_MIN and INT_MAX, You need to define it manually . Write... Directly in the code -2 ** 31 The significance of the equal value is unknown , Should be changed to :
INT_MIN, INT_MAX = -2**31, 2**31 - 1python Middle two slashes “//” It can express division , You don't have to use int() To convert .
python3 Take remainder from middle and take integer from bottom .
python3 The mold taking method is as follows :
Divide positive numbers directly and then take the remainder of the division
-x%y=z
look for -x%y = -x- ( Than -x The small one can be y Negative integer of integer division ) =-x -(-n*y) ==n*y-x=z The mold .
In fact, the previous code only needs to judge the returned y Whether it is in the interval is ok , Of course , There is nothing wrong with judging twice .
because python3 Take negative numbers 10 The module of will also return
Result , You can use this to subtract the module 10, Get a negative number
Such as
, take
, recycling
, That is to say 
# Python3 The modulo operation of x When it is negative, it will also return [0, 9) Results within , Therefore, special judgment is needed here
if x < 0 and digit > 0:
digit -= 10
Finally, the optimized code is as follows :
class Solution:
def reverse(self, x: int) -> int:
INT_MIN, INT_MAX = -2**31, 2**31 - 1
rev = 0
while x != 0:
# INT_MIN It's also a negative number , Can not write rev < INT_MIN // 10
if rev < INT_MIN // 10 + 1 or rev > INT_MAX // 10:
return 0
digit = x % 10
if x < 0 and digit > 0:
digit -= 10
x = (x - digit) // 10
rev = rev * 10 + digit
return rev
perhaps
class Solution:
def reverse(self, x: int) -> int:
INT_MIN, INT_MAX = -2 ** 31, 2 ** 31 - 1
flag = 1 if x > 0 else -1
x_positive = abs(x)
y = 0
if INT_MIN <= x <= INT_MAX:
while x_positive > 0:
temp = x_positive % 10
x_positive = int(x_positive / 10)
y = y * 10 + temp
ret = flag * y
else:
ret = 0
if INT_MIN <= ret <= INT_MAX:
pass
else:
ret = 0
return retMethod 2 : Use string inversion
Set a marker flag, if x>0 Then for 1, Otherwise -1
flag = 1 if x > 0 else -1Let's get back to int Type x Turn into str type , At the same time, reverse the output and then force it to int type
int(str())
utilize abs() Go to x The absolute value of
The final result is multiplied by the mark flag
ret = flag * int(str(abs(x))[::-1])final result
class Solution:
def reverse(self, x: int) -> int:
flag = 1 if x > 0 else -1
ret = flag * int(str(abs(x))[::-1])
if abs(ret) > 2 ** 31:
ret = 0
ret边栏推荐
- 2022暑初二信息竞赛学习成果分享2
- Traditional model predictive control trajectory tracking - circular trajectory (function package has been updated)
- Eight sorts --------- quick sort
- NOI Online 2022普及组 题解&个人领悟
- 滑动窗口 Leetcode 76.最小覆盖子串(困难) 76.76. MinimumWindow Substring (Hard)
- JVM内存结构
- HR must ask questions - how to fight with HR (collected from FPGA Explorer)
- LeetCode #3.无重复字符的最长子串
- 【软件工程之美 - 专栏笔记】25 | 有哪些方法可以提高开发效率?
- 【Leetcode刷题】数组2——二分查找
猜你喜欢

LeetCode #13. 罗马数字转整数

【软件工程之美 - 专栏笔记】19 | 作为程序员,你应该有产品意识

DP1332E多协议高度集成非接触式读写芯片

Ml8 self study notes

LeetCode #977.有序数组的平方

Huawei cloud 14 day Hongmeng device development -day5 drive subsystem development

Joiner.on和stream().map联合使用技巧

markdown与Typora

【RoboMaster】A板接收JY-ME01角度传感器数据--modebus协议&CRC软件校验

Ml self study notes 5
随机推荐
SimpleFOC调参1-力矩控制
【软件工程之美 - 专栏笔记】28 | 软件工程师的核心竞争力是什么?(下)
Rowkey设计
顺序表和链表
Si12T和Si14T低功耗电容触摸芯片
HR must ask questions - how to fight with HR (collected from FPGA Explorer)
JUC collection class is unsafe
crawl笔记
寒假集训总结 (1.23~1.28) [第一梯队]
clickhouse 导入CSV失败 不报错但是无数据
【软件工程之美 - 专栏笔记】19 | 作为程序员,你应该有产品意识
[beauty of software engineering - column notes] 16 | how to write project documents?
LeetCode #344.反转字符串
数学建模心得
Huawei cloud 14 day Hongmeng device development -day3 kernel development
2022 spring recruit - Shanghai an road FPGA post Manager (and Lexin SOC interview)
Ml4 self study notes
【软件工程之美 - 专栏笔记】21 | 架构设计:普通程序员也能实现复杂系统?
Encapsulation - Super keyword
【软件工程之美 - 专栏笔记】14 | 项目管理工具:一切管理问题,都应思考能否通过工具解决