整数反转(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
相关推荐
淡海水几秒前
02-01-原理篇-Mark-Sweep与变种算法
算法·unity·c#·游戏引擎·.net·gc
Navigator_Z3 分钟前
LeetCode //C - 1276. Number of Burgers with No Waste of Ingredients
c语言·算法·leetcode
阿钱真强道6 分钟前
30 嵌入式操作系统 | 上位机界面:Flask 网页显示 + 控制闭环
后端·python·flask
ysu_03147 分钟前
Docker-Desktop-WSL2实战指南-Windows从安装配置到项目容器化
windows·python·docker·容器·composer·dockerfile·wsl2
keyipatience16 分钟前
跳表详细解析
开发语言·数据结构·c++·算法·跳表
想吃火锅100533 分钟前
【leetcode】189.轮转数组js
开发语言·javascript·leetcode
郝学胜-神的一滴33 分钟前
AI 编程智能体 02:AI智能体到底是什么
开发语言·人工智能·python·程序人生·pycharm
大侠归来40 分钟前
cJSON 源码解析:parse_string 函数深入剖析
linux·网络·算法
Guarding and trust41 分钟前
python系列之多线程编程
python
梅雅达编程笔记1 小时前
03_网页表格数据抓取
爬虫·python·beautifulsoup·pandas·数据采集