• 面试题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))
  • 相关阅读:
    036 Python进阶小结
    035 异常处理
    033 数据类型分类
    034 Python深浅拷贝
    032 集合类型内置方法
    031 字典类型内置方法
    XML删除节点
    追加XML
    XML文档的读、写
    XML文档的创建
  • 原文地址:https://www.cnblogs.com/cong12586/p/13299436.html
Copyright © 2020-2023  润新知