面试题16.11跳水板

from typing import List
# 这道题比较容易,遍历一遍k + 1就好了,
# 可以算出所有的结果,但是会有重复值,因此需要将重复值排除
# 同时避免超市
class Solution:
def divingBoard(self, shorter: int, longer: int, k: int) -> List[int]:
# 定义一个列表,用来接收所有的结果
num_list = []
# 当k为0时,需要返回空列表
if k == 0:return []
# 遍历,并算出所有的值
for index1 in range(k + 1):
num = shorter * index1 + (k - index1) * longer
num_list.append(num)
# 先排序
num_list.sort()
tem = []
index = 1
tem.append(num_list[0])
# 然后排除所有的重复值
while index < len(num_list):
if num_list[index] != num_list[index - 1]:
tem.append(num_list[index])
index += 1
else:
index += 1
continue
return tem
A = Solution()
print(A.divingBoard(1,1,100000))
posted @ 2020-07-14 15:42  月为暮  阅读(180)  评论(0编辑  收藏  举报