当前位置:网站首页>[leetcode] integer inversion [7]
[leetcode] integer inversion [7]
2022-07-05 04:58:00 【When camellia flowers bloom.】
problem : To give you one 32 Signed integer of bit x, Return to x The result of reversing the number part in
If the integer after inversion exceeds 32 The range of signed integers of bits [-(2**31), 2**31-1] , Just go back to 0
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
# solution 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
# solution 2
def reverse(x):
if x == 0:
return 0
else:
str_x = str(x)
# Judge x negative
if str_x[0] == '-':
str_x = '-' + str_x[-1:-len(str_x):-1]
if int(str_x) < -(2**31):
return 0
else:
# Judge x In the case of positive numbers
str_x = str_x[-1:- (len(str_x) + 1 ):-1]
if int(str_x) > 2**31 - 1:
return 0
return(int(str_x))边栏推荐
- 【acwing】837. Number of connected block points
- Special information | real estate and office buildings - 22.1.9
- An article takes you to thoroughly understand descriptors
- 2022/7/1学习总结
- Unity and database
- [groovy] closure (closure as function parameter | code example)
- 中国聚氨酯硬泡市场调研与投资预测报告(2022版)
- C # perspective following
- AutoCAD - feature matching
- #775 Div.1 B. Integral Array 数学
猜你喜欢

AutoCAD - isometric annotation
![[groovy] closure (closure call | closure default parameter it | code example)](/img/61/754cee9a940fd4ecd446b38c2f413d.jpg)
[groovy] closure (closure call | closure default parameter it | code example)

54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●

XSS injection

Pdf to DWG in CAD

On-off and on-off of quality system construction

An article takes you to thoroughly understand descriptors

Create a pyGame window with a blue background

AutoCAD - continuous annotation

How to choose a panoramic camera that suits you?
随机推荐
Group counting notes (1) - check code, original complement multiplication and division calculation, floating point calculation
[groovy] closure (closure call | closure default parameter it | code example)
An article takes you to thoroughly understand descriptors
AutoCAD -- dimension break
2020-10-27
#775 Div.1 B. Integral Array 数学
【acwing】528. cheese
2021-10-29
China as resin Market Research and investment forecast report (2022 Edition)
2022 U.S. college students' mathematical modeling e problem ideas / 2022 U.S. game e problem analysis
MD5绕过
PostgreSQL 超越 MySQL,“世界上最好的编程语言”薪水偏低
Flink集群配置
AutoCAD - set layer
Out and ref functions of unity
JVM 原理和流程简介
中国聚氨酯硬泡市场调研与投资预测报告(2022版)
Unity get component
AutoCAD - stretching
cocos_ Lua loads the file generated by bmfont fnt