整数反转(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
相关推荐
用户83562907805112 分钟前
使用 Python 对 Excel 工作表进行排序
后端·python
圣保罗的大教堂19 分钟前
leetcode 301. 删除无效的括号 困难
leetcode
liliangcsdn33 分钟前
派息率指标的应用探索和分析
大数据·算法
for_ever_love__34 分钟前
MySQL 全文索引实战:FULLTEXT、ngram 中文分词与 MATCH AGAINST 到底该怎么用
java·python·mysql·全文检索·分词·索引·ngram
用户83562907805140 分钟前
使用 Python 合并 PowerPoint 演示文稿
后端·python
weixin_461769401 小时前
VS Code 中创建 Jupyter 文件(.ipynb)
ide·python·jupyter
垆边人似月.1 小时前
华为机试题 :两数之和
c++·算法·华为
默默开发1 小时前
物流行业可用算法方向
算法
垆边人似月.1 小时前
华为机试题 :有效的括号
c++·算法·华为
2601_966949651 小时前
多市场量化策略的数据接口应该如何设计:从数据层架构到策略接入
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash