整数反转(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
相关推荐
0思必得012 分钟前
[Web自动化] 反爬虫
前端·爬虫·python·selenium·自动化
嘴贱欠吻!14 分钟前
Flutter鸿蒙开发指南(七):轮播图搜索框和导航栏
算法·flutter·图搜索算法
2301_8223827631 分钟前
Python上下文管理器(with语句)的原理与实践
jvm·数据库·python
张祥64228890437 分钟前
误差理论与测量平差基础笔记十
笔记·算法·机器学习
踩坑记录38 分钟前
leetcode hot100 2.两数相加 链表 medium
leetcode·链表
喵手1 小时前
Python爬虫实战:从零搭建字体库爬虫 - requests+lxml 实战采集字体网字体信息数据(附 CSV 导出)!
爬虫·python·爬虫实战·零基础python爬虫教学·csv导出·采集字体库数据·字体库字体信息采集
qq_192779871 小时前
C++模块化编程指南
开发语言·c++·算法
2301_790300961 小时前
Python深度学习入门:TensorFlow 2.0/Keras实战
jvm·数据库·python
程序员敲代码吗3 小时前
用Python生成艺术:分形与算法绘图
jvm·数据库·python