Skip to content

Latest commit

 

History

History
69 lines (55 loc) · 1.19 KB

7.md

File metadata and controls

69 lines (55 loc) · 1.19 KB
  1. 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 hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

R2

class Solution { public int reverse(int x) { int res = 0; while (x != 0) { int n_res = res * 10 + x % 10; if ((n_res - x % 10) / 10 != res) return 0; x /= 10; res = n_res; } return res; } }

R1

my thoughts:

1. do not know what to say, the biggest 32-bit number is 2147483647

my solution:

**********49ms
class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        s = str(x)
        if s[0] == '-':
            t = s[1:]
            return int(s[0]+t[::-1]) if int(t[::-1])<=2147483647 else 0
        return int(s[::-1]) if int(s[::-1])<2147483647 else 0

my comments:


from other ppl's solution:

1. N/A