当前位置:网站首页>[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))
边栏推荐
- 54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
- Emlog blog theme template source code simple good-looking responsive
- Unity sends messages and blocks indecent words
- 2020-10-27
- AutoCAD - full screen display
- #775 Div.1 C. Tyler and Strings 组合数学
- Séparation et combinaison de la construction du système qualité
- 中国聚氨酯硬泡市场调研与投资预测报告(2022版)
- Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?
- AutoCAD - command repetition, undo and redo
猜你喜欢
2021 Higher Education Club Cup mathematical modeling national tournament ABCD problem - problem solving ideas
django连接数据库报错,这是什么原因
AutoCAD - set layer
2022 thinking of Mathematical Modeling B problem of American college students / analysis of 2022 American competition B problem
Leetcode word search (backtracking method)
2022/7/2做题总结
Number theoretic function and its summation to be updated
54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
Redis 排查大 key 的4种方法,优化必备
【acwing】837. Number of connected block points
随机推荐
Basic knowledge points
Solutions and answers for the 2021 Shenzhen cup
Fluent objects and lists
AutoCAD - stretching
54. Spiral matrix & 59 Spiral matrix II ●●
JVM 原理和流程简介
Recherche de mots pour leetcode (solution rétrospective)
Unity writes timetables (without UI)
#775 Div.1 C. Tyler and Strings 组合数学
数论函数及其求和 待更新
Special information | real estate and office buildings - 22.1.9
【acwing】240. food chain
Research and investment forecast report of adamantane industry in China (2022 Edition)
Unity card flipping effect
2022 thinking of mathematical modeling a problem of American college students / analysis of 2022 American competition a problem
GameObject class and transform class of unity
Unity get component
2021 electrician cup idea + code - photovoltaic building integration plate index development trend analysis and prediction: prediction planning issues
Cocos2dx screen adaptation
Minor spanning tree