当前位置:网站首页>LeetCode 324 摆动排序 II[排序 双指针] HERODING的LeetCode之路
LeetCode 324 摆动排序 II[排序 双指针] HERODING的LeetCode之路
2022-06-28 22:51:00 【HERODING23】

**解题思路:
首先定义一个数组arr与给定数组相同并排序,定义双指针,由于排序是满足一小一大一小一大的顺序,所以可以从思想上把数组分为一大一小的两个序列,要让双指针分别从小序列和大序列最大位置出发,一直到最小,分别放入nums数组中,之所以从最大到最小,而不是从最小到最大,是因为如果序列太短,小序列的最大可能和大序列的最小相邻,而这两如果值相同就不满足了,代码如下:
**
class Solution {
public:
void wiggleSort(vector<int>& nums) {
vector<int> arr = nums;
sort(arr.begin(), arr.end());
int n = arr.size();
int index = 0;
int line = n % 2 == 0 ? n / 2 : n / 2 + 1;
for(int i = line - 1, j = n - 1; i >= 0; i --, j --) {
nums[index ++] = arr[i];
if(j >= line)
nums[index ++] = arr[j];
}
}
};
边栏推荐
- Zadig + SonarQube,为开发过程安全保驾
- CS5463代码模块解析(包含下载链接)
- 【网关开发】ngx嵌套lua时处理CIDR表示的IP地址段
- 在线SQL转HTMLTable工具
- QtCreator5.15.0源码编译全过程记录
- 以产业互联网的发展为开端,行业才能进入到一个全新的发展阶段
- 【HackTheBox】dancing(SMB)
- Powerful open source API interface visual management platform Yapi
- 00 後雲原生工程師:用 Zadig 為思創科技(廣州公交)研發開源節流
- 00 后云原生工程师:用 Zadig 为思创科技(广州公交)研发开源节流
猜你喜欢

Deep virtual memory (VM)

flowable 边界定时器

如何结合均线分析伦敦金行情走势线图

Zadig + 洞态 IAST:让安全溶于持续交付

Production environment sonarqube installation

如何使用伦敦金画出支撑阻力线

直击产业落地 | 飞桨重磅推出业界首个模型选型工具

Zadig + sonarqube, ensuring the safety of the development process

What is low code development?

Zadig officially launched vs code plug-in, making local development more efficient
随机推荐
How to solve the problem of desktop without sound
【Try to Hack】nmap
In the era of industrial Internet, the Internet in the traditional sense will evolve into many new forms
How to use London gold to draw support resistance line
The new version of OpenAPI engine of Kingdee cloud dome is coming!
如何结合均线分析伦敦金行情走势线图
微搭低代码中实现二维码生成
leetCode-栈类型详解
Linux安装mysql5.7(CentOS7.6) 教程
What is low code development?
Career consultation | how to answer the question of career planning during the interview?
Water brother's code
【Try to Hack】nmap
穿越过后,她说多元宇宙真的存在
一文读懂,WMS仓储管理系统与ERP有什么区别
QtCreator5.15.0源码编译全过程记录
With the development of industrial Internet as the starting point, the industry can enter a new stage of development
Prometeus 2.36.0 新特性
What is the prospect of VR panoramic production?
Google Earth Engine(GEE)——利用sentinel-2数据进行农作物提取分析