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)/将每个元素替换为右侧最大元素 [replace-elements-with-greatest-element-on-right-side].html
2022-03-29 12:43:11 +08:00

37 lines
1.2 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>给你一个数组 <code>arr</code> ,请你将每个元素用它右边最大的元素替换,如果是最后一个元素,用 <code>-1</code> 替换。</p>
<p>完成所有替换操作后,请你返回这个数组。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>arr = [17,18,5,4,6,1]
<strong>输出:</strong>[18,6,6,6,1,-1]
<strong>解释:</strong>
- 下标 0 的元素 --> 右侧最大元素是下标 1 的元素 (18)
- 下标 1 的元素 --> 右侧最大元素是下标 4 的元素 (6)
- 下标 2 的元素 --> 右侧最大元素是下标 4 的元素 (6)
- 下标 3 的元素 --> 右侧最大元素是下标 4 的元素 (6)
- 下标 4 的元素 --> 右侧最大元素是下标 5 的元素 (1)
- 下标 5 的元素 --> 右侧没有其他元素,替换为 -1
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>arr = [400]
<strong>输出:</strong>[-1]
<strong>解释:</strong>下标<strong> </strong>0 的元素右侧没有其他元素。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>5</sup></code></li>
</ul>