|
| 1 | +--- |
| 2 | +title: 2525.根据规则将箱子分类 |
| 3 | +date: 2023-10-20 12:16:14 |
| 4 | +tags: [题解, LeetCode, 简单, 数学] |
| 5 | +--- |
| 6 | + |
| 7 | +# 【LetMeFly】2525.根据规则将箱子分类:优雅解法? |
| 8 | + |
| 9 | +力扣题目链接:[https://leetcode.cn/problems/categorize-box-according-to-criteria/](https://leetcode.cn/problems/categorize-box-according-to-criteria/) |
| 10 | + |
| 11 | +<p>给你四个整数 <code>length</code> ,<code>width</code> ,<code>height</code> 和 <code>mass</code> ,分别表示一个箱子的三个维度和质量,请你返回一个表示箱子 <strong>类别</strong> 的字符串。</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li>如果满足以下条件,那么箱子是 <code>"Bulky"</code> 的: |
| 15 | + |
| 16 | + <ul> |
| 17 | + <li>箱子 <strong>至少有一个</strong> 维度大于等于 <code>10<sup>4</sup></code> 。</li> |
| 18 | + <li>或者箱子的 <strong>体积</strong> 大于等于 <code>10<sup>9</sup></code> 。</li> |
| 19 | + </ul> |
| 20 | + </li> |
| 21 | + <li>如果箱子的质量大于等于 <code>100</code> ,那么箱子是 <code>"Heavy"</code> 的。</li> |
| 22 | + <li>如果箱子同时是 <code>"Bulky"</code> 和 <code>"Heavy"</code> ,那么返回类别为 <code>"Both"</code> 。</li> |
| 23 | + <li>如果箱子既不是 <code>"Bulky"</code> ,也不是 <code>"Heavy"</code> ,那么返回类别为 <code>"Neither"</code> 。</li> |
| 24 | + <li>如果箱子是 <code>"Bulky"</code> 但不是 <code>"Heavy"</code> ,那么返回类别为 <code>"Bulky"</code> 。</li> |
| 25 | + <li>如果箱子是 <code>"Heavy"</code> 但不是 <code>"Bulky"</code> ,那么返回类别为 <code>"Heavy"</code> 。</li> |
| 26 | +</ul> |
| 27 | + |
| 28 | +<p><strong>注意</strong>,箱子的体积等于箱子的长度、宽度和高度的乘积。</p> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | + |
| 32 | +<p><strong>示例 1:</strong></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +<b>输入:</b>length = 1000, width = 35, height = 700, mass = 300 |
| 36 | +<b>输出:</b>"Heavy" |
| 37 | +<b>解释:</b> |
| 38 | +箱子没有任何维度大于等于 10<sup>4 </sup>。 |
| 39 | +体积为 24500000 <= 10<sup>9 </sup>。所以不能归类为 "Bulky" 。 |
| 40 | +但是质量 >= 100 ,所以箱子是 "Heavy" 的。 |
| 41 | +由于箱子不是 "Bulky" 但是是 "Heavy" ,所以我们返回 "Heavy" 。</pre> |
| 42 | + |
| 43 | +<p><strong>示例 2:</strong></p> |
| 44 | + |
| 45 | +<pre> |
| 46 | +<b>输入:</b>length = 200, width = 50, height = 800, mass = 50 |
| 47 | +<b>输出:</b>"Neither" |
| 48 | +<b>解释:</b> |
| 49 | +箱子没有任何维度大于等于 10<sup>4</sup> 。 |
| 50 | +体积为 8 * 10<sup>6</sup> <= 10<sup>9</sup> 。所以不能归类为 "Bulky" 。 |
| 51 | +质量小于 100 ,所以不能归类为 "Heavy" 。 |
| 52 | +由于不属于上述两者任何一类,所以我们返回 "Neither" 。</pre> |
| 53 | + |
| 54 | +<p> </p> |
| 55 | + |
| 56 | +<p><strong>提示:</strong></p> |
| 57 | + |
| 58 | +<ul> |
| 59 | + <li><code>1 <= length, width, height <= 10<sup>5</sup></code></li> |
| 60 | + <li><code>1 <= mass <= 10<sup>3</sup></code></li> |
| 61 | +</ul> |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +## 方法一:优雅解法? |
| 66 | + |
| 67 | +判断箱子是否符合```bulky```或```heavy```很简单,对于一些编程语言注意不要“32位整数溢出”就可以了。 |
| 68 | + |
| 69 | +得到了值为```0```或```1```的两个变量```bulky```和```heavy```,怎么“优雅”地转为字符串返回呢? |
| 70 | + |
| 71 | +可以预先定义一个字符串数组```dic = ['Neither', 'Heavy', 'Bulky', 'Both']```,这样直接返回```dic[bulky * 2 + heavy]```就可以了。本质上是将这两个变量看成了```dic```下标二进制下的低两位,这样就避免了四个```if-else```的出现。 |
| 72 | + |
| 73 | ++ 时间复杂度$O(1)$ |
| 74 | ++ 空间复杂度$O(1)$ |
| 75 | + |
| 76 | +### AC代码 |
| 77 | + |
| 78 | +#### C++ |
| 79 | + |
| 80 | +```cpp |
| 81 | +const string dict[4] = {"Neither", "Heavy", "Bulky", "Both"}; |
| 82 | + |
| 83 | +typedef long long ll; |
| 84 | +class Solution { |
| 85 | +public: |
| 86 | + string categorizeBox(ll length, ll width, ll height, ll mass) { |
| 87 | + bool bulky = length >= 10000 || width >= 10000 || height >= 10000 || length * width * height >= 1000000000; |
| 88 | + bool heavy = mass >= 100; |
| 89 | + return dict[bulky * 2 + heavy]; |
| 90 | + } |
| 91 | +}; |
| 92 | +``` |
| 93 | + |
| 94 | +#### Python |
| 95 | + |
| 96 | +```python |
| 97 | +dic = ['Neither', 'Heavy', 'Bulky', 'Both'] |
| 98 | +class Solution: |
| 99 | + def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str: |
| 100 | + bulky = length >= 10000 or width >= 10000 or height >= 10000 or length * width * height >= 1000000000 |
| 101 | + heavy = mass >= 100 |
| 102 | + return dic[bulky * 2 + heavy] |
| 103 | + |
| 104 | +``` |
| 105 | + |
| 106 | +> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2023/10/20/LeetCode%202525.%E6%A0%B9%E6%8D%AE%E8%A7%84%E5%88%99%E5%B0%86%E7%AE%B1%E5%AD%90%E5%88%86%E7%B1%BB/)哦~ |
| 107 | +> Tisfy:[https://letmefly.blog.csdn.net/article/details/133943695](https://letmefly.blog.csdn.net/article/details/133943695) |
0 commit comments