整数反转(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
相关推荐
爱昏羔3 小时前
下篇:从检索到交互 — 物流RAG问答系统与WebUI全实现
python·langchain·agent
_Jimmy_3 小时前
Agent引用数据库知识过时的增量同步方案
人工智能·python·langchain
min(a,b)4 小时前
学习第 4 天:面向对象与异常处理
python·学习·学习方法
拳里剑气6 小时前
C++算法:BFS解决FloodFill算法
c++·算法·bfs·宽度优先
yangshicong6 小时前
第19章:AI安全防护与AI安全
人工智能·python·安全·prompt·ai编程
果汁华6 小时前
Function Calling 与 Python 实战完整指南
开发语言·网络·python
c_lb72886 小时前
2026年不同基础做量化,先找AI能参与的位置
人工智能·python
wanderist.7 小时前
Lambda表达式在算法竞赛中的应用
java·开发语言·算法
稚南城才子,乌衣巷风流8 小时前
支配树(Dominator Tree)详解:概念、算法与应用
算法