整数反转(leetcode)

题目:

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。

如果反转后整数超过 32 位的有符号整数的范围 −231, 231 − 1 ,就返回 0。

假设环境不允许存储 64 位整数(有符号或无符号)。

示例 1:

输入:x = 123

输出:321

python 复制代码
class Solution:
    def reverse(self, x: int) -> int:
       x_str = str(x)
       x_str = x_str.strip('-')
       x_str = x_str.rstrip('0')
       x_str = x_str[::-1]
       if x < 0:
         x_str = '-' + x_str
       if not x_str:
         x_str = '0'
       result = int(x_str)
       if result < -2**31 or result > 2**31 - 1:
         return 0
       return result
相关推荐
全栈练习生几秒前
大模型原理之 KV Cache
python·ai
天赐范式12 分钟前
天赐范式第183天:让选择开始生效——育种者方程与选择生效条件
python·数字生命·天赐范式·动态运行时·育种者方程·选择压力·遗传力
mmmmath_318 分钟前
LeetCode.020.有效的括号
数据结构·算法·leetcode
Doubbbbbbble云39 分钟前
算法优化中的数学建模与理论界限分析4
算法·数学建模
benchmark_cc40 分钟前
批量行情返回后,怎样把请求失败的股票和正常数据分开?
python·数据分析·pandas·量化交易·股票数据·quantdash
垆边人似月.41 分钟前
华为机考题:字符个数统计
数据结构·算法·华为
en.en..1 小时前
Linux 进程退出exit(函数)
java·开发语言·算法
All for pursuit.1 小时前
【回溯-2】22.括号生成
数据结构·c++·算法·leetcode
y = xⁿ1 小时前
关于Agent工程落地
前端·人工智能·python
longlongzihan1 小时前
LeetCode 1004.最大连续1的个数III 滑动窗口及动态规划题解
c++·算法·leetcode·dp·滑动窗口