当前位置:网站首页>[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))
边栏推荐
- Emlog blog theme template source code simple good-looking responsive
- 669. 修剪二叉搜索树 ●●
- 2021 huashubei mathematical modeling idea + reference + paper
- Minor spanning tree
- Common database statements in unity
- stm32Cubemx(8):RTC和RTC唤醒中断
- Flink集群配置
- Leetcode word search (backtracking method)
- Introduction to JVM principle and process
- 中国溶聚丁苯橡胶(SSBR)行业研究与预测报告(2022版)
猜你喜欢
AutoCAD - set layer
2022 thinking of mathematical modeling D problem of American college students / analysis of 2022 American competition D problem
Solutions and answers for the 2021 Shenzhen cup
669. 修剪二叉搜索树 ●●
Use assimp library to read MTL file data
Autocad-- Real Time zoom
Unity3d learning notes
2022 American College Students' mathematical modeling ABCDEF problem thinking /2022 American match ABCDEF problem analysis
Leetcode word search (backtracking method)
Special information | real estate and office buildings - 22.1.9
随机推荐
AutoCAD - scaling
PR first time
Unity intelligent NPC production -- pre judgment walking (method 1)
Unity sends messages and blocks indecent words
Introduce Hamming distance and calculation examples
Dotween usage records ----- appendinterval, appendcallback
2020-10-27
PostgreSQL 超越 MySQL,“世界上最好的编程语言”薪水偏低
中国AS树脂市场调研与投资预测报告(2022版)
This article is good
Use assimp library to read MTL file data
AutoCAD - isometric annotation
Pause and resume of cocos2dx Lua scenario
MySQL audit log Archive
[groovy] closure (closure call | closure default parameter it | code example)
猿人学第一题
Pdf to DWG in CAD
2021 electrician Cup - high speed rail traction power supply system operation data analysis and equivalent modeling ideas + code
cocos_ Lua listview loads too much data
Panel panel of UI