整数反转(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
相关推荐
m沐沐1 小时前
【深度学习】YOLOv2目标检测算法——改进点、网络结构与聚类先验框解析
人工智能·pytorch·深度学习·算法·yolo·目标检测·transformer
不会就选b1 小时前
算法日常・每日刷题--<链表>3
数据结构·算法·链表
geovindu2 小时前
go: Iterative Algorithms
开发语言·后端·算法·golang·迭代算法
郭老二3 小时前
【Python】基本语法:装饰器语法糖@
python
Zachery Pole4 小时前
CCF-CSP备战NO.7【队列】
算法
闪电悠米4 小时前
力扣hot100-48.旋转图像-转置翻转详解
算法·leetcode·职场和发展
_Jimmy_4 小时前
Agent常用检索器的详细介绍
python·langchain
满天星83035775 小时前
【算法】最长递增子序列(三种解法)
算法
小柯南敲键盘5 小时前
图片翻译API接入与自动化实现指南
运维·python·自动化
旅僧5 小时前
Q-learning(自用)
python·机器学习