当前位置:网站首页>LeetCode 324 擺動排序 II[排序 雙指針] HERODING的LeetCode之路
LeetCode 324 擺動排序 II[排序 雙指針] HERODING的LeetCode之路
2022-06-28 22:55: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];
}
}
};
边栏推荐
- WMS仓库管理系统模块之波次拣货
- 基于graph-linked embedding的多组学单细胞数据整合与调控推理
- Detailed steps for MySQL to recover data through IBD files
- k线图基础知识图解——单根K线的含义
- torch.nn.Transformer导入失败
- 时间序列预测系列文章总结(代码使用方法)
- 复杂嵌套的对象池(4)——管理多类实例和多阶段实例的对象池
- Post-00 cloud native Engineer: use Zadig to increase revenue and reduce expenditure for the R & D of Sichuang Technology (Guangzhou public transport)
- Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
- Detailed explanation of Zadig's self-test and joint debugging sub environment for developers
猜你喜欢

QtCreator5.15.0源码编译全过程记录

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

台式机没声音怎么样才能解决

DBNN实验进展

windows mysql5.7 开启binlog日志

Lecun predicts AgI: big model and reinforcement learning are both ramps! My world model is the new way

深入虚拟内存(Virtual Memory,VM)

基于graph-linked embedding的多组学单细胞数据整合与调控推理

在线SQL转HTMLTable工具

Encounter with avita 11: long lost freshness under strong product power
随机推荐
Qsrand, srand random number generating function in qt5.15 has been discarded
Ingénieur natif du nuage après 00: utiliser Zadig pour développer des sources ouvertes et des économies d'énergie pour la technologie innovante (bus de Guangzhou)
C#/VB. Net to convert PDF to excel
【Try to Hack】nmap
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
时间序列预测系列文章总结(代码使用方法)
What is the difference between WMS warehouse management system and ERP
[deep learning] (3) encoder mechanism in transformer, complete pytoch code attached
Flowable boundary timer
PyTorch搭建Transformer实现多变量多步长时间序列预测(负荷预测)
云计算的迷路者
Encounter with avita 11: long lost freshness under strong product power
如何结合均线分析伦敦金行情走势线图
计数排序的简单理解
After crossing, she said that the multiverse really exists
华为云GaussDB(for Redis)揭秘第19期:六大秒级能力盘点
Post-00 cloud native Engineer: use Zadig to increase revenue and reduce expenditure for the R & D of Sichuang Technology (Guangzhou public transport)
一文搞懂shell脚本
【HackTheBox】dancing(SMB)
Progress of dbnn experiment