# -*- coding:utf-8 -*- classSolution: defjumpFloor(self, number): # write code here if number == 1: return1 if number == 2: return2 return self.jumpFloor(number - 1) + self.jumpFloor(number - 2)
正确解法:
defjumpFloor(self, number): # write code here if number == 1: return1 if number == 2: return2 a, b = 1, 2 i = 2 while i < number: a, b = b, a + b i += 1 return b
classSolution: """第二种,假设有这个数字,那么它的数量一定比其它所有数字之和还要多,按照这个思路得出num,然后验证 """ defMoreThanHalfNum_Solution(self, numbers): # write code here ifnot numbers: return0 num = numbers[0] count = 1 for i inrange(1, len(numbers)): if numbers[i] == num: count += 1 else: count -= 1 if count == 0: num = numbers[i] count = 1 count = 0 for i in numbers: if i == num: count += 1 return num if count > len(numbers) / 2.0else0
或者
# -*- coding:utf-8 -*- classSolution: defMoreThanHalfNum_Solution(self, numbers): # write code here last = 0 last_count = 0
for num in numbers: if last_count == 0: last = num last_count += 1 else: if num == last: last_count += 1 else: last_count -= 1 if last_count == 0: return0 else: last_count = 0 for num in numbers: if num == last: last_count += 1 return last if last_count > len(numbers) // 2else0