comments | difficulty | edit_url |
---|---|---|
true |
简单 |
你正在使用一堆木板建造跳水板。有两种类型的木板,其中长度较短的木板长度为shorter
,长度较长的木板长度为longer
。你必须正好使用k
块木板。编写一个方法,生成跳水板所有可能的长度。
返回的长度需要从小到大排列。
示例:
输入: shorter = 1 longer = 2 k = 3 输出: {3,4,5,6}
提示:
- 0 < shorter <= longer
- 0 <= k <= 100000
如果
如果
否则,我们可以使用长度为
时间复杂度
class Solution:
def divingBoard(self, shorter: int, longer: int, k: int) -> List[int]:
if k == 0:
return []
if longer == shorter:
return [longer * k]
ans = []
for i in range(k + 1):
ans.append(longer * i + shorter * (k - i))
return ans
class Solution {
public int[] divingBoard(int shorter, int longer, int k) {
if (k == 0) {
return new int[0];
}
if (longer == shorter) {
return new int[] {longer * k};
}
int[] ans = new int[k + 1];
for (int i = 0; i < k + 1; ++i) {
ans[i] = longer * i + shorter * (k - i);
}
return ans;
}
}
class Solution {
public:
vector<int> divingBoard(int shorter, int longer, int k) {
if (k == 0) return {};
if (longer == shorter) return {longer * k};
vector<int> ans;
for (int i = 0; i < k + 1; ++i)
ans.push_back(longer * i + shorter * (k - i));
return ans;
}
};
func divingBoard(shorter int, longer int, k int) []int {
if k == 0 {
return []int{}
}
if longer == shorter {
return []int{longer * k}
}
var ans []int
for i := 0; i < k+1; i++ {
ans = append(ans, longer*i+shorter*(k-i))
}
return ans
}
function divingBoard(shorter: number, longer: number, k: number): number[] {
if (k === 0) {
return [];
}
if (longer === shorter) {
return [longer * k];
}
const ans: number[] = [k + 1];
for (let i = 0; i <= k; ++i) {
ans[i] = longer * i + shorter * (k - i);
}
return ans;
}
class Solution {
func divingBoard(_ shorter: Int, _ longer: Int, _ k: Int) -> [Int] {
if k == 0 {
return []
}
if shorter == longer {
return [shorter * k]
}
var ans = [Int](repeating: 0, count: k + 1)
for i in 0...k {
ans[i] = longer * i + shorter * (k - i)
}
return ans
}
}