【Leetcode-算法-Python3】7. 反转整数

思路:

1.确定x位数

2.取余

3.累加

4.溢出判断

class Solution:
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        flag = 0
        if x < 0:
            flag = 1
        x = abs(x)
        s = str(x)
        count = len(s)
        re = 0
        t1 = count
        t2 = count - 1
        while x > 0:
            k = x % 10
            x = x // 10
            re = re + k * pow(10,t2)
            t2 -= 1
        if str(re) == str(pow(2,31)) and flag == 0:
            return 0
        if float(re) > float(pow(2,31)):
            return 0
        if flag == 1:
            re = 0 - re
        return re

1.1

猜你喜欢

转载自blog.csdn.net/aawsdasd/article/details/80765052