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))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/86779.html
標籤:Python
上一篇:Python基礎-檔案讀寫
