LeetCode 0007-Reverse Integer

題目如下:

Given a 32-bit signed integer, reverse digits of an integer.

只求 pass 的解答。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
func reverse(x int) int {
	if x == 0 {
		return 0
	}

	bound := 2147483647

	isPositive := x > 0
	if !isPositive {
		x *= -1
		bound++
	}

	ans := 0
	for x > 0 {
		ans = ans*10 + x%10
		if ans > bound {
			return 0
		}
		x /= 10
	}

	if isPositive {
		return ans
	}
	return -ans
}

過去曾用 python 寫的解法。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        maxint = 2 ** 31 - 1
        minint = -1 * (2 ** 31)
        if x > maxint or x < minint:
            return 0
        operator = 1 if x > 0 else -1
        absx = math.fabs(x)
        result = 0
        while absx > 0:
            digit = absx % 10
            result = result * 10 + digit
            absx = (absx - digit) // 10
    
        if minint <= result <= maxint:
            return int(operator * result)
        return 0
updatedupdated2020-10-022020-10-02