当前位置:网站首页>Leetcode 324 Swing sort II [tri double pointeur] le chemin du leetcode pour l'héroding
Leetcode 324 Swing sort II [tri double pointeur] le chemin du leetcode pour l'héroding
2022-06-28 22:55:00 【Heroding23】

**Comment résoudre le problème:
Définissez d'abord un tableauarrIdentique au tableau donné et trié,Définir un double pointeur,Parce que le tri est de satisfaire l'ordre d'un petit à un grand,Il est donc possible de diviser mentalement le tableau en deux séquences plus grandes et plus petites,Pour que le double pointeur commence à la position maximale de la petite et de la grande séquence respectivement,Jusqu'au plus petit,Mettre séparémentnumsDans le tableau,La raison pour laquelle nous passons du plus grand au plus petit,Pas du plus petit au plus grand,Parce que si la séquence est trop courte,La plus grande probabilité de petites séquences et la plus petite contiguïté de grandes séquences,Et ces deux - là ne sont pas satisfaits si les valeurs sont les mêmes,Les codes sont les suivants::
**
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];
}
}
};
边栏推荐
- Wechat red envelope cover making tutorial and use guide with link jump
- 在QT进行cin(全网最清晰教程)
- WMS仓库管理系统模块之波次拣货
- Redis+AOP+自定义注解实现限流
- 论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》
- Detailed explanation of Zadig's self-test and joint debugging sub environment for developers
- 一文搞懂shell脚本
- LeCun预言AGI:大模型和强化学习都是斜道!我的世界模型才是新路
- Oracle deleting archived logs and adding scheduled tasks
- Yiming Anke submitted a statement to the Hong Kong Stock Exchange: the loss doubled in 2021, and the past financing amount was exaggerated
猜你喜欢
Sample code of using redis to realize the like function

生产环境sonarqube安装

在线文本过滤小于指定长度工具

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

Post-00 cloud native Engineer: use Zadig to increase revenue and reduce expenditure for the R & D of Sichuang Technology (Guangzhou public transport)

00 后云原生工程师:用 Zadig 为思创科技(广州公交)研发开源节流

This simple little function saves 213 hours for our production research team in half a year

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

Panxiaoming, senior vice president of IC nansha|amd and President of Greater China: process, architecture and platform optimization break through the computing boundary

Detailed explanation of Zadig's self-test and joint debugging sub environment for developers
随机推荐
Huawei cloud gaussdb (for redis) unveiling issue 19: inventory of six second level capabilities
超级工厂里的生意图鉴
Production environment sonarqube installation
Is it safe and reliable to open a securities account in changtou school?
【深度学习】(2) Transformer 网络解析,代码复现,附Pytorch完整代码
Business atlas in super factory
Steady! How thousands of micro services can quickly access Zadig (helm chart)
How powerful is the Zadig build? Practice together
Mysql通过ibd文件恢复数据的详细步骤
Detailed steps for MySQL to recover data through IBD files
How to solve the problem of desktop without sound
Wechat red envelope cover making tutorial and use guide with link jump
Can we still enter the "pit" data analysis now? Look at the hot jobs of data analysis in 2022!
[gateway development] handle the IP address segment represented by CIDR when NGX nested Lua
Encounter with avita 11: long lost freshness under strong product power
With the development of industrial Internet as the starting point, the industry can enter a new stage of development
keil工程,程序写多后,RTT不能打印
计数排序的简单理解
Qt5.15中qsrand,srand随机数生成函数已弃用问题
微搭低代码中实现二维码生成