checking integer overflow in python

后端 未结 6 627
故里飘歌
故里飘歌 2021-02-05 08:01
class Solution(object):
    def reverse(self, x):
        \"\"\"
        :type x: int
        :rtype: int
        \"\"\"
        negative = False
        if(x < 0):
          


        
6条回答
  •  孤独总比滥情好
    2021-02-05 08:31

    class Solution:
        def reverse(self, x: int) -> int:
    
            split = [i for i in str(x)]
            split = split[::-1]
            final = ''
    
            
            if split[-1]=='-':
                final += '-'
                
                for i in split[0:-1]:
                    print(i)
                    final+=i
            else:
                
                for i in split[0:]:
                    final+=i
                    
            final = int(final)
            
            if(abs(final) > (2 ** 31 - 1)):
                    return 0
                    
            return(final)
    

提交回复
热议问题