当前位置:网站首页>[LeetCode] 整数反转【7】
[LeetCode] 整数反转【7】
2022-07-05 04:55:00 【山茶花开时。】
问题: 给你一个32位的有符号整数x,返回将x中的数字部分反转后的结果
如果反转后整数超过32位的有符号整数的范围[-(2**31), 2**31-1] ,就返回0
示例1
输入: x = 123
输出: 321
示例2
输入: x = -123
输出: -321
示例3
输入: x = 120
输出: 21
示例4
输入: x = 0
输出: 0
# 解法1
def reverse(x):
if x == 0:
return 0
if x > 0:
str_x = str(x)
str_x = str_x[::-1]
x = int(str_x)
if x > 2**31 - 1:
return 0
if x < 0:
str_x = str(x)
str_x = str_x[1:]
str_x = str_x[::-1]
x = '-' + str_x
x = int(x)
if x < -(2**31):
return 0
return x
# 解法2
def reverse(x):
if x == 0:
return 0
else:
str_x = str(x)
# 判断x为负数的情况
if str_x[0] == '-':
str_x = '-' + str_x[-1:-len(str_x):-1]
if int(str_x) < -(2**31):
return 0
else:
# 判断x为正数的情况
str_x = str_x[-1:- (len(str_x) + 1 ):-1]
if int(str_x) > 2**31 - 1:
return 0
return(int(str_x))边栏推荐
- Minor spanning tree
- AutoCAD - Document Management
- Research and investment forecast report of adamantane industry in China (2022 Edition)
- China as resin Market Research and investment forecast report (2022 Edition)
- Common technologies of unity
- mysql審計日志歸檔
- 质量体系建设之路的分分合合
- MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP
- Rip notes [rip message security authentication, increase of rip interface measurement]
- [ideas] 2021 may day mathematical modeling competition / May Day mathematical modeling ideas + references + codes
猜你喜欢

Introduce Hamming distance and calculation examples

Introduction to JVM principle and process

MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP
![[groovy] closure (closure as function parameter | code example)](/img/a6/a4ed401acfb61f85eb08daa15a8a80.jpg)
[groovy] closure (closure as function parameter | code example)

Download the details and sequence of the original data access from the ENA database in EBI

Number theoretic function and its summation to be updated

AutoCAD - lengthening

3dsmax scanning function point connection drawing connection line

xss注入

Unity get component
随机推荐
Flink cluster configuration
Unity parallax infinite scrolling background
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
2021 higher education social cup mathematical modeling national tournament ABCD questions - problem solving ideas - Mathematical Modeling
【acwing】528. cheese
Leetcode 222 number of nodes of complete binary tree
Out and ref functions of unity
Unity synergy
次小生成树
中国金刚烷行业研究与投资预测报告(2022版)
Introduction to JVM principle and process
China as resin Market Research and investment forecast report (2022 Edition)
AutoCAD - graphic input and output
MySQL audit log archiving
Function template
Recherche de mots pour leetcode (solution rétrospective)
Personal required code
Understand encodefloatrgba and decodefloatrgba
Detailed introduction of OSPF header message
2020-10-27