整数反转(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
相关推荐
爱学大树锯几秒前
1361 · 文字并排
算法
Tisfy8 分钟前
LeetCode 2483.商店的最少代价:两次遍历 -> 一次遍历
算法·leetcode·题解·遍历
YGGP18 分钟前
【Golang】LeetCode 279. 完全平方数
算法·leetcode
38242782721 分钟前
python3网络爬虫开发实战 第二版:绑定回调
开发语言·数据库·python
im_AMBER24 分钟前
Leetcode 87 等价多米诺骨牌对的数量
数据结构·笔记·学习·算法·leetcode
dagouaofei31 分钟前
培训项目总结 PPT 工具对比评测,哪款更专业
python·powerpoint
Hello eveybody31 分钟前
用代码生成你的电影预告片(Python)
python
月明长歌33 分钟前
【码道初阶】Leetcode771 宝石与石头:Set 判成员 vs List 判成员(同题两种写法的差距)
java·数据结构·leetcode·list·哈希算法·散列表
import_random34 分钟前
[算法]时间序列(介绍)
算法
wuk99838 分钟前
MATLAB中求解和分析马蒂厄方程
人工智能·算法·matlab