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)/构造元素不等于两相邻元素平均值的数组 [array-with-elements-not-equal-to-average-of-neighbors].html
2022-03-29 12:43:11 +08:00

37 lines
1.5 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

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>给你一个 <strong>下标从 0 开始</strong> 的数组 <code>nums</code> ,数组由若干 <strong>互不相同的</strong> 整数组成。你打算重新排列数组中的元素以满足:重排后,数组中的每个元素都 <strong>不等于</strong> 其两侧相邻元素的 <strong>平均值</strong></p>
<p>更公式化的说法是,重新排列的数组应当满足这一属性:对于范围&nbsp;<code>1 &lt;= i &lt; nums.length - 1</code> 中的每个 <code>i</code> <code>(nums[i-1] + nums[i+1]) / 2</code> <strong>不等于</strong> <code>nums[i]</code> 均成立 。</p>
<p>返回满足题意的任一重排结果。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [1,2,3,4,5]
<strong>输出:</strong>[1,2,4,5,3]
<strong>解释:</strong>
i=1, nums[i] = 2, 两相邻元素平均值为 (1+4) / 2 = 2.5
i=2, nums[i] = 4, 两相邻元素平均值为 (2+5) / 2 = 3.5
i=3, nums[i] = 5, 两相邻元素平均值为 (4+3) / 2 = 3.5
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [6,2,0,9,7]
<strong>输出:</strong>[9,7,6,2,0]
<strong>解释:</strong>
i=1, nums[i] = 7, 两相邻元素平均值为 (9+6) / 2 = 7.5
i=2, nums[i] = 6, 两相邻元素平均值为 (7+2) / 2 = 4.5
i=3, nums[i] = 2, 两相邻元素平均值为 (6+0) / 2 = 3
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>