整数反转(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
相关推荐
qq_4579242914 小时前
[rdk系列之情绪识别算法上板运行]
算法
本妖精不是妖精14 小时前
在 CentOS 7 上部署 Node.js 18 + Claude Code
linux·python·centos·node.js·claudecode
Vanranrr14 小时前
Python vs PowerShell:自动化 C++ 配置文件的两种实现方案
c++·python·自动化
Bdygsl14 小时前
数字图像处理总结 Day 3 —— 图像增强与运算
图像处理·算法
田里的水稻14 小时前
spline_curve
算法·几何学
andwhataboutit?14 小时前
cuda环境安装
python
子午14 小时前
【交通标志识别系统】Python+TensorFlow+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
测试老哥14 小时前
Postman接口测试
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman
X***C86214 小时前
SpringMVC 请求参数接收
前端·javascript·算法
棒棒的皮皮14 小时前
【OpenCV】Python图像处理之掩膜
图像处理·python·opencv·计算机视觉