美文网首页
leetcode7. 整数反转 python实现

leetcode7. 整数反转 python实现

作者: vvblack4 | 来源:发表于2020-02-17 16:37 被阅读0次

    题目:

    leetcode7题目描述

    解法:

    这道题我们采用先将整数转换成字符串,对字符串反转,再将字符串转换回整数

    具体代码如下:

    class Solution:
        def reverse(self, x: int) -> int:
            flag = 0
            if x<0:
                x=-x
                flag = 1
            #print(x)
            temp = str(x)[::-1]
            if(int(temp) < pow(-2,31)-1 or int(temp)>pow(2,31)):
                return 0
            if flag == 1:
                return -int(temp)
            return int(temp)
    

    相关文章

      网友评论

          本文标题:leetcode7. 整数反转 python实现

          本文链接:https://www.haomeiwen.com/subject/kjzbfhtx.html