1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/最后一块石头的重量 [last-stone-weight].html
2022-03-29 12:43:11 +08:00

33 lines
1.3 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>有一堆石头,每块石头的重量都是正整数。</p>
<p>每一回合,从中选出两块<strong> 最重的</strong> 石头,然后将它们一起粉碎。假设石头的重量分别为 <code>x</code> 和 <code>y</code>,且 <code>x <= y</code>。那么粉碎的可能结果如下:</p>
<ul>
<li>如果 <code>x == y</code>,那么两块石头都会被完全粉碎;</li>
<li>如果 <code>x != y</code>,那么重量为 <code>x</code> 的石头将会完全粉碎,而重量为 <code>y</code> 的石头新重量为 <code>y-x</code></li>
</ul>
<p>最后,最多只会剩下一块石头。返回此石头的重量。如果没有石头剩下,就返回 <code>0</code></p>
<p> </p>
<p><strong>示例:</strong></p>
<pre>
<strong>输入:</strong>[2,7,4,1,8,1]
<strong>输出:</strong>1
<strong>解释:</strong>
先选出 7 和 8得到 1所以数组转换为 [2,4,1,1,1]
再选出 2 和 4得到 2所以数组转换为 [2,1,1,1]
接着是 2 和 1得到 1所以数组转换为 [1,1,1]
最后选出 1 和 1得到 0最终数组转换为 [1],这就是最后剩下那块石头的重量。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= stones.length <= 30</code></li>
<li><code>1 <= stones[i] <= 1000</code></li>
</ul>