当前位置:网站首页>[LeetCode] 整数反转【7】
[LeetCode] 整数反转【7】
2022-07-05 04:55:00 【山茶花开时。】
问题: 给你一个32位的有符号整数x,返回将x中的数字部分反转后的结果
如果反转后整数超过32位的有符号整数的范围[-(2**31), 2**31-1] ,就返回0
示例1
输入: x = 123
输出: 321
示例2
输入: x = -123
输出: -321
示例3
输入: x = 120
输出: 21
示例4
输入: x = 0
输出: 0
# 解法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
# 解法2
def reverse(x):
if x == 0:
return 0
else:
str_x = str(x)
# 判断x为负数的情况
if str_x[0] == '-':
str_x = '-' + str_x[-1:-len(str_x):-1]
if int(str_x) < -(2**31):
return 0
else:
# 判断x为正数的情况
str_x = str_x[-1:- (len(str_x) + 1 ):-1]
if int(str_x) > 2**31 - 1:
return 0
return(int(str_x))边栏推荐
- Number theoretic function and its summation to be updated
- How much do you know about 3DMAX rendering skills and HDRI light sources? Dry goods sharing
- How to choose a panoramic camera that suits you?
- Leetcode 222 number of nodes of complete binary tree
- Forecast report on research and investment prospects of Chinese wormwood industry (2022 Edition)
- LeetCode之單詞搜索(回溯法求解)
- XSS injection
- Difference between singleton and factory pattern
- xss注入
- mysql審計日志歸檔
猜你喜欢

Number theoretic function and its summation to be updated
![[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)](/img/03/329adb314606f29c8a4cb2260e84c8.jpg)
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
![[Business Research Report] top ten trends of science and technology and it in 2022 - with download link](/img/9f/4fc63fa7b0e9afc5dd638d4b599b2c.jpg)
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link
![[groovy] closure (closure call | closure default parameter it | code example)](/img/61/754cee9a940fd4ecd446b38c2f413d.jpg)
[groovy] closure (closure call | closure default parameter it | code example)

3dsmax scanning function point connection drawing connection line

计组笔记(1)——校验码、原补码乘除计算、浮点数计算

Autocad-- Real Time zoom
![[crampon game] MC tutorial - first day of survival](/img/81/82034c0382f545c39bd8c15f132ec7.jpg)
[crampon game] MC tutorial - first day of survival
![Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]](/img/e7/f699ee982ea325b8d04f8bd467a559.jpg)
Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]

【acwing】240. food chain
随机推荐
Unity synergy
54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
Detailed explanation of the ranking of the best universities
2022 thinking of mathematical modeling a problem of American college students / analysis of 2022 American competition a problem
[groovy] closure (closure call | closure default parameter it | code example)
[groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)
AutoCAD - isometric annotation
AutoCAD - full screen display
[groovy] closure closure (customize closure parameters | customize a single closure parameter | customize multiple closure parameters | specify the default value of closure parameters)
AutoCAD - Center zoom
On-off and on-off of quality system construction
Unity parallax infinite scrolling background
Manually implement heap sorting -838 Heap sort
Mode in BST (binary tree & Notes on question brushing)
Difference between singleton and factory pattern
This article is good
Unity connects to the database
Leetcode word search (backtracking method)
Minor spanning tree
Introduction to JVM principle and process