整数反转(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
相关推荐
LJHclasstore_luo6 小时前
【C++题解】112354.平行时空
数据结构·c++·算法
sa100276 小时前
搭建京东评论监控系统,自动捕捉新增评价,快速挖掘用户真实痛点
python
weixin_461769407 小时前
anaconda安装pytorch安装python
人工智能·pytorch·python
梅雅达编程笔记7 小时前
编程启蒙|Scratch 转 Python 系列第10天:问答闯关游戏实战(AI题库管理+随机出题实战)
人工智能·python·游戏·青少年编程
AOwhisky7 小时前
Python 学习笔记(第十一期)——运维自动化(上·后篇):进程级监控与子进程管理——psutil进阶
运维·开发语言·python·学习·云原生·运维开发
人工干智能7 小时前
Python 的链式调用:一连串的“点”调用
开发语言·python
卷无止境7 小时前
Cognee:面向 AI 智能体的开源记忆平台
人工智能·python
weixin_BYSJ19877 小时前
django在线图书销售平台---附源码16192
java·javascript·spring boot·python·django·flask·php
小陈phd8 小时前
QAnything 阅读优化策略03——查询转换
算法
夜瞬8 小时前
内生可解释性:从黑盒深度模型到可理解、可干预的智能系统
人工智能·python