JAVA面试题 跳水板

Posted diehuacanmeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA面试题 跳水板相关的知识,希望对你有一定的参考价值。

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

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

示例:

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

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

思路:首先可以考虑特殊情况,长模板和短木板长度相同的情况下,必然只有一种可能性。长短木板长度不相同的情况下我们可以发现最后输出结果的个数会是木板总数+1

class Solution {
    public int[] divingBoard(int shorter, int longer, int k) {
        if(k == 0) return new int[] {};
		int intLength = k + 1;
		if(shorter == longer) intLength = 1;
		int[] board = new int[intLength];
		if(shorter == longer) {
			board[0] = shorter * k;
		}else {
			for(int i = 0; i <= k; i++) {
				int boardLong = i * longer + (k - i) * shorter;
				board[i] = boardLong;
			}
		}
		return board;
    }
}

  

以上是关于JAVA面试题 跳水板的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 面试题 16.11. 跳水板 | Python

程序员面试金典-面试题 16.11. 跳水板

LeetCode 面试题 16.11.. 跳水板 模拟

面试题 16.11. 跳水板(leetcode)-7月8日

LeetCode面试题 16.11. 跳水板

外观模式+LeetCode.面试题 16.11