Python笔试题:给定一个整数数组和一个目标值、找出数组中为2个俩个数、若无返回-1【杭州多测师】【杭州多测师_王sir】
class Test: def func(self): ''' 给定一个整数数组和一个目标值、找出数组中为2个俩个数、若无返回-1 :return: ''' list1=[-1,-2,4,3,1,0,2] # list1 = [-1,4,5] new=[] for i in range(len(list1)): for j in range(len(list1)-i-1): if list1[i]+list1[j] == 2: new.append((list1[i],list1[j])) if new != []: print(new) else: print(-1) def reverse(self, x): a = 2**31-1 #int32最大值是:2147483647 b = -(2**31) #int32最小值是:-2147483648 n = 0 while x != 0: m = -(abs(x)%10) if x < 0 else x%10 x = int(x/10) if n > int(a/10) or n == int(a/10) and m > 7: # int32最大值是2147483647,最后一位是7 return 0 if n < int(b/10) or n == int(b/10) and m < -8: # int32最小值是-2147483648,最后一位是8 return 0 n = n*10 + m return n t = Test() t.func() #第一个题目 print(t.reverse(341)) #第二个题目