整数反转(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
相关推荐
加宝的起点雷达20 分钟前
40 条候选里 38 条来自同一个源,主卡整屏重样
人工智能·算法·创业
贝壳69021 分钟前
bool-hybrid-array 9.13.6 偷偷加了个"按键自动升序"的字典,我翻出来测了下
python
秋天的怀念_David28 分钟前
算法一千零一夜 · 第二十六夜 协议的母语
java·spring boot·算法
艺杯羹35 分钟前
MCP本地服务集成避坑:从Stdio管道僵死到双向JSON-RPC稳健通信
人工智能·python·ai·系统架构·大语言模型·ai编程·mcp
happylifetree39 分钟前
Python78:核心语法-面向对象基础-类与对象
python
duansamve1 小时前
Python中进程
python
文丑颜不良啊1 小时前
LeetCode2021~2040题目解析
java·leetcode
日日大王学编程2 小时前
AI Agent 技术发展历程、演进逻辑与技术范式综述
人工智能·python
春涧草茶2 小时前
慢就是快16-1协程
开发语言·前端·python