leetCode练题——7. Reverse Integer
1、题目:
7. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123 Output: 321
Example 2:
Input: -123 Output: -321
Example 3:
Input: 120 Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
2、我的解法:
1 # -*- coding: utf-8 -*- 2 # @Time : 2020/1/26 12:33 3 # @Author : SmartCat0929 4 # @Email : 1027699719@qq.com 5 # @Link : https://github.com/SmartCat0929 6 # @Site : 7 # @File : 7. Reverse Integer.py 8 class Solution: 9 def reverse(self, x: int) -> int: 10 if x >= -2147483648 and x <= 2147483647: 11 if x >= 0: 12 r = "" 13 else: 14 r = "-" 15 w = abs(x) 16 y = str(w) 17 n = len(y) 18 d = [] 19 for i in (y): 20 d.append(i) 21 for j in range(n): 22 c = d.pop() 23 r = r + c 24 v = int(r) 25 if v >= -2147483648 and v <= 2147483647: 26 return v 27 else: 28 return 0 29 else: 30 return 0
    既能朝九晚五,又能浪迹天涯!

    
                
            
        
浙公网安备 33010602011771号