当前位置:网站首页>[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))边栏推荐
- Panel panel of UI
- mysql审计日志归档
- Use assimp library to read MTL file data
- Cocos progress bar progresstimer
- China needle coke industry development research and investment value report (2022 Edition)
- Dotween usage records ----- appendinterval, appendcallback
- AutoCAD - set layer
- This article is good
- Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?
- PostgreSQL 超越 MySQL,“世界上最好的编程语言”薪水偏低
猜你喜欢

質量體系建設之路的分分合合
![Rip notes [rip message security authentication, increase of rip interface measurement]](/img/89/f70af97676496d7b9aa867be89f11d.jpg)
Rip notes [rip message security authentication, increase of rip interface measurement]

Recherche de mots pour leetcode (solution rétrospective)

【acwing】837. Number of connected block points

AutoCAD - window zoom
![[groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)](/img/36/c4206a95c007e41df628d99e06ba18.jpg)
[groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)

AutoCAD - continuous annotation

PostgreSQL 超越 MySQL,“世界上最好的编程语言”薪水偏低

LeetCode之單詞搜索(回溯法求解)

AutoCAD - command repetition, undo and redo
随机推荐
2021-10-29
Animation
[groovy] closure (closure as function parameter | code example)
Unity enables mobile phone vibration
Basic knowledge points of dictionary
JVM 原理和流程简介
【Leetcode】1352. Product of the last K numbers
Unity shot tracking object
#775 Div.1 C. Tyler and Strings 组合数学
How to choose a panoramic camera that suits you?
2021 electrician cup idea + code - photovoltaic building integration plate index development trend analysis and prediction: prediction planning issues
Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?
Séparation et combinaison de la construction du système qualité
Pause and resume of cocos2dx Lua scenario
"Measuring curve length" of CAD dream drawing
cocos2dx_ Lua particle system
【Leetcode】1352. 最后 K 个数的乘积
Emlog博客主题模板源码简约好看响应式
Vs2015 secret key
Lua determines whether the current time is the time of the day