题目:
示例 1:
输入: 123
输出: 321
示例 2:
输入: -123
输出: -321
示例 3:
输入: 120
输出: 21
注意:
假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-integer
代码:
class Solution:
def reverse(self, x: int) -> int:
#先将输入的有符号整数,转换成字符串形式
str_x=str(x)
#将得到的经过转换的字符串,转换为列表的形式,便于进行列表元素的翻转操作
list_x=list(str_x)
#将列表中的元素进行翻转
list_x.reverse()
#删除列表中位于非零元素前的所有零元素
for i in range(len(list_x)):
if list_x[i]==0:
continue
else:
list_x=list_x[i:]
break
#判断输入的有符号整数的正负
if list_x[-1]=='