当前位置:网站首页>[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))
边栏推荐
- Common technologies of unity
- Time format conversion
- AutoCAD - stretching
- [groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)
- 2022/7/2做题总结
- Unity synergy
- The first topic of ape Anthropology
- Database under unity
- Introduce Hamming distance and calculation examples
- 中国聚氨酯硬泡市场调研与投资预测报告(2022版)
猜你喜欢
Pdf to DWG in CAD
Unity find the coordinates of a point on the circle
2021 Higher Education Club Cup mathematical modeling national tournament ABCD problem - problem solving ideas
Autocad-- dynamic zoom
Number theoretic function and its summation to be updated
Panel panel of UI
[groovy] closure (closure call | closure default parameter it | code example)
Introduction to JVM principle and process
2022 American College Students' mathematical modeling ABCDEF problem thinking /2022 American match ABCDEF problem analysis
2022 thinking of mathematical modeling C problem of American college students / analysis of 2022 American competition C problem
随机推荐
Unity sends messages and blocks indecent words
用 Jmeter 工具做个小型压力测试
Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?
The first topic of ape Anthropology
Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area
PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low
AutoCAD - full screen display
2021 electrician cup idea + code - photovoltaic building integration plate index development trend analysis and prediction: prediction planning issues
AutoCAD - Zoom previous
[groovy] closure (closure call | closure default parameter it | code example)
Flutter tips: various fancy nesting of listview and pageview
Autocad-- dynamic zoom
Animation
54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
The difference between heap and stack
Thinking of 2022 American College Students' mathematical modeling competition
PR first time
SQLServer 存储过程传递数组参数
中国溶聚丁苯橡胶(SSBR)行业研究与预测报告(2022版)
Sixth note