今天的题目是
7.整数反转,如图
每日leetcode(4)
一开始我是踩了很多坑的,比如列表不能转整数,判断是否带符号,只有列表可以reverse()和remove(),输出的是整数,而且输出的数也要在规定范围内
后来我改了很久,得到了下面的代码,还算不错

class Solution():
    def rever(self,x):
        if x<-2**31|x>2**31-1:
            return 0
        if x==0:
            return 0
        s=str(x)
        if s[0]=='-':
            s='-'+s[-1:0:-1]
            if -2**31<int(s)<2**31-1:
                return int(s)
            else:
                return 0
        else:
            s=s[::-1]
            if -2**31<int(s)<2**31-1:
                return int(s)
            else:
                return 0

下图是leetcode测试的
每日leetcode(4)
每日leetcode(4)

相关文章:

  • 2022-01-04
  • 2021-12-12
  • 2021-11-22
  • 2021-12-13
  • 2021-10-21
  • 2021-07-19
  • 2021-08-07
  • 2021-08-24
猜你喜欢
  • 2021-12-27
  • 2021-08-07
  • 2021-09-06
  • 2021-08-11
  • 2021-11-03
  • 2021-04-14
相关资源
相似解决方案