标签:public nbsp back 地址 ++ color span short href
地址 https://leetcode-cn.com/problems/diving-board-lcci/
你正在使用一堆木板建造跳水板。有两种类型的木板,其中长度较短的木板长度为shorter,长度较长的木板长度为longer。你必须正好使用k块木板。编写一个方法,生成跳水板所有可能的长度。 返回的长度需要从小到大排列。 示例: 输入: shorter = 1 longer = 2 k = 3 输出: {3,4,5,6} 提示: 0 < shorter <= longer 0 <= k <= 100000
算法1
模拟题?
先计算出上下限,然后一次使用短木棒替换长木棒。注意木棒长短一致的边界问题
C++ 代码
class Solution { public: vector<int> divingBoard(int shorter, int longer, int k) { vector<int> ans; if (k == 0) return ans; int minlen = shorter * k; ans.push_back(minlen); int maxlen = longer * k; int currlen = minlen - shorter + longer; while (currlen <= maxlen && currlen > minlen) { ans.push_back(currlen); currlen = currlen - shorter + longer; } return ans; } };
标签:public nbsp back 地址 ++ color span short href
原文地址:https://www.cnblogs.com/itdef/p/13266213.html