整数反转(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
相关推荐
std787914 分钟前
用PYTHON实现俄罗斯方块游戏案例
python·游戏·pygame
Dream it possible!19 分钟前
LeetCode 面试经典 150_二叉树层次遍历_二叉树的层平均值(82_637_C++_简单)
c++·leetcode·面试·二叉树
小羊失眠啦.20 分钟前
Rust核心库(core)深度解析:无依赖基石的设计与实践
数据库·算法·rust
Wenhao.22 分钟前
LeetCode Hot100 每日温度
数据结构·算法·leetcode·golang
吃着火锅x唱着歌24 分钟前
LeetCode 1679.K和数对的最大数目
算法·leetcode·职场和发展
im_AMBER26 分钟前
Leetcode 57
笔记·学习·算法·leetcode
im_AMBER27 分钟前
Leetcode 58 | 附:滑动窗口题单
笔记·学习·算法·leetcode
sin_hielo28 分钟前
leetcode 2154
算法·leetcode
Sunhen_Qiletian34 分钟前
YOLO的再进步---YOLOv3算法详解(上)
算法·yolo·计算机视觉