整数反转(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
相关推荐
Ronaldinho Gaúch11 分钟前
算法题中的日期问题
开发语言·c++·算法
踩坑记录13 分钟前
leetcode ho100 124. 二叉树中的最大路径和 hard
leetcode
喵手16 分钟前
Python爬虫实战:小红书热门笔记爬虫实战 - 搜索关键词驱动的内容采集指南!
爬虫·python·爬虫实战·零基础python爬虫教学·小红书热门笔记·搜索关键词·采集小红书热门笔记数据
Chary201620 分钟前
Opencascade VTK 集成服务 VIS
算法
nita张41 分钟前
战略定位实战:案例分享与经验总结
大数据·人工智能·python
楠秋9201 小时前
代码随想录算法训练营第三十一天|56. 合并区间 、 738.单调递增的数字、968.监控二叉树
数据结构·算法·leetcode·贪心算法
MadPrinter1 小时前
Python 异步爬虫实战:FindQC 商品数据爬取系统完整教程
爬虫·python·算法·自动化
清水白石0081 小时前
Python 函数式编程实战:从零构建函数组合系统
开发语言·python
郝学胜-神的一滴1 小时前
Effective Modern C++ 条款36:如果有异步的必要请指定std::launch::async
开发语言·数据结构·c++·算法
小此方1 小时前
Re:从零开始的 C++ STL篇(六)一篇文章彻底掌握C++stack&queue&deque&priority_queue
开发语言·数据结构·c++·算法·stl