https://leetcode-cn.com/problems/reverse-integer/

代码:

# -*- coding:utf-8 -*-
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
if x < (-2 ** 31) or x > (2 ** 31 - 1):
return 0
if x < 0:
str_x = str(-x)
res = -int(str_x[::-1])
if res < (-2 ** 31) or res > (2 ** 31 - 1):
return 0
return -int(str_x[::-1])
str_x = str(x)
res = int(str_x[::-1])
if res < (-2 ** 31) or res > (2 ** 31 - 1):
return 0
return res


if __name__ == '__main__':
x = 1534236469
print(Solution().reverse(x))

思路:这道题没啥可说的,是个简单题,但是值得一说的就是这个32位有符号整数,超过算移除,需要在每次返回钱判断是否发生溢出。

分类: 算法

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用 * 标注