整数反转(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
相关推荐
XLYcmy32 分钟前
TarGuessIRefined密码生成器详细分析
开发语言·数据结构·python·网络安全·数据安全·源代码·口令安全
王老师青少年编程35 分钟前
2025年12月GESP(C++二级): 环保能量球
c++·算法·gesp·csp·信奥赛·二级·环保能量球
weixin_4334176741 分钟前
Canny边缘检测算法原理与实现
python·opencv·算法
梨落秋霜1 小时前
Python入门篇【元组】
android·数据库·python
i小杨1 小时前
python 项目相关
开发语言·python
CoderCodingNo1 小时前
【GESP】C++五级真题(贪心思想考点) luogu-P11960 [GESP202503 五级] 平均分配
开发语言·c++·算法
weixin_462446231 小时前
使用 Tornado + systemd 搭建图片静态服务(imgserver)
开发语言·python·tornado
别多香了1 小时前
python基础之面向对象&异常捕获
开发语言·python
youngee111 小时前
hot100-61电话号码的字母组合
java·数据结构·leetcode
POLITE31 小时前
Leetcode 76.最小覆盖子串 JavaScript (Day 6)
javascript·算法·leetcode