当前位置:网站首页>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;
}
};
边栏推荐
- CVPR 2022 | neural radiation field geometry editing method nerf editing
- Kubernetes binary installation (v1.20.15) (VII) plug a work node
- Implementation of VGA protocol based on FPGA
- Customize terrain providers (terrain plugin framework) -04
- Terraformer importing cloud resources
- BS-XX-007基于JSP实现户籍管理系统
- 全球手机市场衰退,连苹果也对iPhone14不抱过高期待
- Introduction to long connection
- Terrain + ansible implements infrastructure and configuration management
- No delay / no delay live video instance effect cases
猜你喜欢

可变参表达式

Unsealing easy QF PDA helps enterprises improve ERP management

Is byte really the end of the universe?

JSP实现银柜台业务绩效考核系统

Energy storage operation and configuration analysis of high proportion wind power system (realized by Matlab)

二十八-三维点云实时和离线生成二维栅格、三维栅格地图

Using vscode code code template to improve mobx coding efficiency
![[signal processing] digital signal processing matlab design with GUI interface and report](/img/bd/7bcb03b5619998dda4c07bbf6d6436.png)
[signal processing] digital signal processing matlab design with GUI interface and report

Tp6 whoops based exception takeover (beautiful interface)

Introduction to long connection
随机推荐
Ecplise cannot connect to SQL Server
CVPR 2022 | neural radiation field geometry editing method nerf editing
Using vscode code code template to improve mobx coding efficiency
InfoQ geek media's 15th anniversary essay solicitation - dynamic ranking of mobile terminal development [mui+flask+mongodb]
高比例风电电力系统储能运行及配置分析(Matlab实现)
论文导读 | 机器学习在数据库基数估计中的应用
couldn‘t upgrade db schema: insert into ACT_GE_PROPERTY values (‘common.sche[已解决]
Live share experience
How to quickly compress the size of video?
How can tampermonkey replace flash player with H5 player?
cadence SPB17.4 - group operation(add to group, view group list, delete group)
create_ Error in engine MySQL connector encryption method
Powerful full text search tool anytxt searcher
Business practice of volcano engine cloud database VEDB in bytes
Question bank and answers for 2022 tool fitter (intermediate) operation certificate examination
三级分类展示
[signal de-noising] chromatographic baseline estimation and de-noising based on sparsity (beads) with matlab code and papers
d的each与map不一致
复选框 全选or取消全选
Unity 检测对象是否在相机的视锥体范围内