class Solution {
public:
string categorizeBox(int length, int width, int height, int mass) {
}
};
2525. 根据规则将箱子分类
给你四个整数 length
,width
,height
和 mass
,分别表示一个箱子的三个维度和质量,请你返回一个表示箱子 类别 的字符串。
"Bulky"
的:
104
。109
。100
,那么箱子是 "Heavy"
的。"Bulky"
和 "Heavy"
,那么返回类别为 "Both"
。"Bulky"
,也不是 "Heavy"
,那么返回类别为 "Neither"
。"Bulky"
但不是 "Heavy"
,那么返回类别为 "Bulky"
。"Heavy"
但不是 "Bulky"
,那么返回类别为 "Heavy"
。注意,箱子的体积等于箱子的长度、宽度和高度的乘积。
示例 1:
输入:length = 1000, width = 35, height = 700, mass = 300 输出:"Heavy" 解释: 箱子没有任何维度大于等于 104 。 体积为 24500000 <= 109 。所以不能归类为 "Bulky" 。 但是质量 >= 100 ,所以箱子是 "Heavy" 的。 由于箱子不是 "Bulky" 但是是 "Heavy" ,所以我们返回 "Heavy" 。
示例 2:
输入:length = 200, width = 50, height = 800, mass = 50 输出:"Neither" 解释: 箱子没有任何维度大于等于 104 。 体积为 8 * 106 <= 109 。所以不能归类为 "Bulky" 。 质量小于 100 ,所以不能归类为 "Heavy" 。 由于不属于上述两者任何一类,所以我们返回 "Neither" 。
提示:
1 <= length, width, height <= 105
1 <= mass <= 103
原站题解
rust 解法, 执行用时: 0 ms, 内存消耗: 2.1 MB, 提交时间: 2023-10-20 07:37:42
impl Solution { pub fn categorize_box(length: i32, width: i32, height: i32, mass: i32) -> String { let x = length >= 10000 || width >= 10000 || height >= 10000 || length as i64 * width as i64 * height as i64 >= 1000000000; let y = mass >= 100; if x && y { return "Both".to_string(); } if x { return "Bulky".to_string(); } if y { return "Heavy".to_string(); } "Neither".to_string() } }
javascript 解法, 执行用时: 60 ms, 内存消耗: 40.8 MB, 提交时间: 2023-10-20 07:37:19
/** * @param {number} length * @param {number} width * @param {number} height * @param {number} mass * @return {string} */ var categorizeBox = function(length, width, height, mass) { const x = length >= 1e4 || width >= 1e4 || height >= 1e4 || length * width * height >= 1e9; const y = mass >= 100; if (x && y) return "Both"; if (x) return "Bulky"; if (y) return "Heavy"; return "Neither"; };
cpp 解法, 执行用时: 4 ms, 内存消耗: 6.1 MB, 提交时间: 2023-10-20 07:37:00
class Solution { public: string categorizeBox(int length, int width, int height, int mass) { bool x = length >= 10000 || width >= 10000 || height >= 10000 || 1LL * length * width * height >= 1000000000; bool y = mass >= 100; if (x && y) return "Both"; if (x) return "Bulky"; if (y) return "Heavy"; return "Neither"; } };
java 解法, 执行用时: 0 ms, 内存消耗: 38.7 MB, 提交时间: 2023-10-20 07:36:49
class Solution { public String categorizeBox(int length, int width, int height, int mass) { boolean x = length >= 10000 || width >= 10000 || height >= 10000 || (long) length * width * height >= 1000000000; boolean y = mass >= 100; if (x && y) return "Both"; if (x) return "Bulky"; if (y) return "Heavy"; return "Neither"; } }
golang 解法, 执行用时: 0 ms, 内存消耗: 1.9 MB, 提交时间: 2023-01-09 10:23:38
func categorizeBox(length, width, height, mass int) string { x := length >= 1e4 || width >= 1e4 || height >= 1e4 || length*width*height >= 1e9 y := mass >= 100 switch { case x && y: return "Both" case x: return "Bulky" case y: return "Heavy" default: return "Neither" } }
python3 解法, 执行用时: 32 ms, 内存消耗: 15 MB, 提交时间: 2023-01-09 10:23:13
class Solution: def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str: x = length >= 10000 or width >= 10000 or height >= 10000 or length * width * height >= 10 ** 9 y = mass >= 100 if x and y: return "Both" if x: return "Bulky" if y: return "Heavy" return "Neither"