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.

class Solution: def reverse(self, x): """ :type x: int :rtype: int """ list_x = [] if x >0 : for str_x in str(x).split(): list_x.append(str_x) r_list_x = list_x[::-1] for item in list_x: r_str_x = "".join(item) return (int (r_str_x))

What is wrong with this one:

x = 123,

str(x).split() is still “123”

split is split the spaces…

Advertisements