当前位置:网站首页>Leetcode 1968. 构造元素不等于两相邻元素平均值的数组(可以,终于解决)
Leetcode 1968. 构造元素不等于两相邻元素平均值的数组(可以,终于解决)
2022-06-11 14:02:00 【我不是萧海哇~~~~】

给你一个 下标从 0 开始 的数组 nums ,数组由若干 互不相同的 整数组成。你打算重新排列数组中的元素以满足:重排后,数组中的每个元素都 不等于 其两侧相邻元素的 平均值 。
更公式化的说法是,重新排列的数组应当满足这一属性:对于范围 1 <= i < nums.length - 1 中的每个 i ,(nums[i-1] + nums[i+1]) / 2 不等于 nums[i] 均成立 。
返回满足题意的任一重排结果。
示例 1:
输入:nums = [1,2,3,4,5]
输出:[1,2,4,5,3]
解释:
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
示例 2:
输入:nums = [6,2,0,9,7]
输出:[9,7,6,2,0]
解释:
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
提示:
- 3 <= nums.length <= 10^5
- 0 <= nums[i] <= 10^5
主要思路:
我这边想的是,两大夹一小,所谓两大夹一小就是先添加大的元素,然后添加一小元素,再添加一大元素,所以先对元素进行排序就可以按照此思路添加到新的数组中即可(其实就是摆动序列才能解决)
Code:
class Solution {
public:
vector<int> rearrangeArray(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<int>res;
int end=nums.size();
int start=0;
for(int i=0;i<nums.size();i++)
{
if((i%2)==0)
{
res.push_back(nums[--end]);
}
else
res.push_back(nums[start++]);
}
return res;
}
};
边栏推荐
- d的each与map不一致
- Unsealing easy QF PDA helps enterprises improve ERP management
- Code comparison tool, I use these six
- How to write high-performance code (IV) optimize data access
- 强大的全文本搜索工具——AnyTXT Searcher
- SQL: how to use the data of purchase order and sales order to calculate commodity cost by moving weighted average method
- Alibaba Cloud 3 (Soaring Falcon) x86_64(Py3.7.8) 系统 YUM源
- 解决循环依赖BUG。Relying upon circular references is discouraged and they are prohibited by default.
- vim二次替换
- Single table query of SQL data query
猜你喜欢

Ecplise cannot connect to SQL Server

cadence SPB17.4 - allegro - allegro_free_viewer

Musk says he doesn't like being a CEO, but rather wants to do technology and design; Wu Enda's "machine learning" course is about to close its registration | geek headlines

高比例风电电力系统储能运行及配置分析(Matlab实现)

基于FPGA的VGA协议实现

Introduction to reverse learning - excellent assembly debugging tool OllyDbg

Operating instructions for communication between RS485 (Modbus RTU) industrial RFID reader ck-fr03-a01 and PLC Mitsubishi fx5u

cadence SPB17.4 - group operation(add to group, view group list, delete group)

全球手机市场衰退,连苹果也对iPhone14不抱过高期待
![[Clickhouse] the clckhouse view can be inserted but not queried](/img/72/717d70af49be2b1dc2331fe603d106.jpg)
[Clickhouse] the clckhouse view can be inserted but not queried
随机推荐
About uni app configuration, app does not display the top title bar setting
Explanation of waitgroup usage in go language learning
RS485(Modbus RTU)工业RFID读写器CK-FR03-A01与PLC三菱FX5U的通讯操作说明
[signal de-noising] chromatographic baseline estimation and de-noising based on sparsity (beads) with matlab code and papers
SQL: how to use the data of purchase order and sales order to calculate commodity cost by moving weighted average method
Redis uses 10 tips, please accept!
Terraformer导入云上资源
【Flink】Flink CancellationException null DefaultExecutionGraphCache LeaderRetrievalHandler
Three level classification display
BS-XX-007基于JSP实现户籍管理系统
The global mobile phone market is declining, and even apple does not expect too much of the iphone14
Can't understand kotlin source code? Starting with the contracts function~
C language learning record 6
Work summary: it took a long time to write SQL because of Cartesian product problem (Cartesian product summary attached)
How to manage the server to make the website stable and smooth
d的each与map不一致
Introduction to long connection
三级分类展示
Jdbctemplate data background management. I don't know why roleid is empty when adding users
d区间到可空转换