【Python】【难度:简单】Leetcode 面试题 16.11. 跳水板

你正在使用一堆木板建造跳水板。有两种类型的木板,其中长度较短的木板长度为shorter,长度较长的木板长度为longer。你必须正好使用k块木板。编写一个方法,生成跳水板所有可能的长度。

返回的长度需要从小到大排列。

示例:

输入:
shorter = 1
longer = 2
k = 3
输出: {3,4,5,6}
提示:

0 < shorter <= longer
0 <= k <= 100000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/diving-board-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution(object):
    def divingBoard(self, shorter, longer, k):
        """
        :type shorter: int
        :type longer: int
        :type k: int
        :rtype: List[int]
        """
        if not k:
            return []

        s=set()
        for i in range(0,k+1):
            s.add(i*shorter+(k-i)*longer)
        return sorted(list(s))

执行结果:

通过

显示详情

执行用时 :152 ms, 在所有 Python 提交中击败了18.18%的用户

内存消耗 :21.4 MB, 在所有 Python 提交中击败了100.00%的用户

原创文章 105 获赞 0 访问量 1648

猜你喜欢

转载自blog.csdn.net/thomashhs12/article/details/106117431