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.

Difficulty: introductory

Starter code:

class Solution:
    def reverse(self, x: int) -> int:
        

Solutions:
class Solution:
     def reverse(self, x):
         """
         :type x: int
         :rtype: int
         """
         if x < 0:
             y = -1 * int(str(-x)[::-1])
         else:
             y = int(str(x)[::-1])  
             
         if y > 2**31 or y < -2**31:
             y = 0
         return y

class Solution:
     def reverse(self, x):
         """
         :type x: int
         :rtype: int
         """
         
         if x>=0:
             x=str(x)[::-1]
             x=int(x)+0
         else:
             x=str(-x)[::-1]
             x=0-int(x)
         if x<-2**31 or x>=2**31-1:
             x=0
         return x
         """
         x = str(x)
         if x[0] == '-':
             x = x[1:]
             ans = int()
             """

class Solution:
     def reverse(self, x):
         """
         :type x: int
         :rtype: int
         """
         if x > 0:
             x = int(str(x)[::-1])
         elif x < 0:
             x = 0 - int(str(abs(x))[::-1])
         if -2 ** 31 < x < 2 ** 31:
             return x
         else:
             return 0

class Solution:
     def reverse(self, x):
         """
         :type x: int
         :rtype: int
         """
         str_x = str(x)
         n = len(str_x)
         if str_x[0] != '-':
             y = int(str_x[::-1])  #[a:b:c] from a to exclusive b by c
         else:
             y = -int(str_x[:0:-1])
         
         return(y*(y.bit_length() < 32))


class Solution:
     def reverse(self, x):
         """
         :type x: int
         :rtype: int
         """
         if x == 0:
             return 0
 
         s = -1 if x < 0 else 1
         v = int(str(abs(x))[::-1])
 
         if s == -1:
             t = -v & -0x80000000
             if t == -0x80000000:
                 return -v
             else:
                 return 0
         else:
             t = v & 0x7fffffff
             if t == v:
                 return v
             else:
                 return 0


class Solution:
     def reverse(self, x):
         """
         :type x: int
         :rtype: int
         """
         flag = x < 0
         p = x
         if flag == True:
             p = -1 * x
 
         digit = []
         while p != 0:
             digit.append(p % 10)
             p = p // 10
 
         n = 0
         for i in digit:
             n = n * 10 + i
 
         if -2 ** 31 <= n <= 2 ** 31 - 1:
             return n if flag == False else -1 * n
 
         return 0

class Solution:
     flag = 0
     def reverse(self, x):
             if x < 0:
                 self.flag = 1
                 self.x = str(-x)
             else:
                 self.x = str(x)
 
             list1 = []
 
             for i in self.x:
                 if i != 0:
                     self.flag1 = 1
                 if self.flag1 == 1:
                     list1.insert(0,i)
             self.x = int("".join(list1))
 
             if self.flag == 1 :
                 self.x = -self.x
             if self.x <= 2147483647 and self.x >= -2147483648:
                 return self.x
             else:
                 return 0

class Solution:
     def reverse(self, x):
         """
         :type x: int
         :rtype: int
         """
         sign = 1
         if x<0:
             x=-x
             sign = -1
         result = 0
         while x>0:
             end = x%10
             result = result*10 + end
             x = x//10
         result *= sign
         if result > 2**31-1 or result < -2**31:
             return 0
         else:
             return result

class Solution:
     def reverse(self, x):
         """
         :type x: int
         :rtype: int
         """
         if x.bit_length()>32 :
     
             print("wrong");
         else:
             number = [];
             digit_num = [];
             new_num = 0;
       
             error = abs(x);
             for i in range(len(str(abs(x)))):
                 if not number:
                     digit = abs(x)//10**(len(str(abs(x)))-1);    
                     number.append(str(digit));
                     digit_num.append(digit);
                 else:
                     error = error - digit_num[i-1]*10**(len(str(abs(x)))-i);
                     digit = error//10**(len(str(abs(x)))-1-i);                  
                     number.append(str(digit));
                     digit_num.append(digit);
                     
             number.reverse();
           
             while(int(number[0])==0 and len(number)>1):
                 del number[0];
                 
             for i in range(len(number)):
                 new_num = new_num + int(number[i])*10**(len(number)-i-1);
             if new_num.bit_length()>=32:
                 return 0;
             
             if x<0:
                 new_num = -new_num;
             return new_num;

class Solution:
     def reverse(self, x):
         str_x = str(x)
 
         if x >= 0 and int(str_x[::-1]) <= (2**31-1):
             return int(str_x[::-1])
         elif x < 0 and -int(str_x[:0:-1]) >= (-2**31):
             return -int(str_x[:0:-1])
         else:
             return 0

class Solution:
     def reverse(self, x):
         """
         :type x: int
         :rtype: int
         """
         if x is 0:
             return 0;
         ori_str = str(x)
         answer = 0
         if ori_str[0] is '-':
             answer = -1 * int(ori_str[:0:-1].lstrip('0'))
         else:
             answer = int(ori_str[::-1].lstrip('0'));
         
         if answer >= 2 ** 31 or answer < -(2 ** 31):
             return 0
         else:
             return answer

class Solution(object):
     def reverse(self, x):
         """
         :type x: int
         :rtype: int
         """
         if x>=0:
             sign=1
         else:
             sign=-1
         x=abs(x)
         reserve=0
         while x>0:
             reminder= x%10
             reserve= reserve*10+reminder
             x=x//10
         if reserve>(2**31-1):
             reserve=0
         return reserve*sign

class Solution:
     def reverse(self, x):
         """
         :type x: int
         :rtype: int
         """
         if x==0:
             return 0
         if x>0:
             sign=1
         else:
             sign=-1
             x=0-x
         
         bits=[]
         for i in range (10):
             bits.append(x%10)
             x=x//10
         print (bits)
 
         left_i=0
         right_i=9
 
         for i in range (10):
             if bits[i]==0:
                 left_i=left_i+1
             else:
                 break
 
         for i in range (9,-1,-1):
             if bits[i]==0:
                 right_i=right_i-1
             else:
                 break
 
         print (left_i,right_i)
 
         factor=1
         result=0
         for i in range (right_i,left_i-1,-1):
             result=result+factor*bits[i]
             factor=factor*10
 
         result=result*sign
         
         if result>2147483647 or result<-2147483648:
             return 0
         else:
             return result