如何用Python解决,有什么解决思路

img


用Python中反转给定的整数,🈶何解决思路方法,更好的解决这个问题有什么思路,什么思路,运用什么

先将整型转换为字符列表,利用列表的反转方法,最后将字符列表转成整型


n = 98653214
s = list(str(n))
s.reverse()
reverse_num = int("".join(s))
print(reverse_num)

def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        num = 0
        if x == 0:
            return 0
        if x < 0:
            x = -x
            while x != 0:
                num = num*10 + x%10
                x = x/10
            num = -num
        else:
            while x != 0:
                num = num*10 + x%10
                x = x/10
            
        if num>pow(2,31)-1 or num < pow(-2,31):
            return 0
        return num