当前位置:网站首页>LeetCode每日一题——324. 摆动排序 II
LeetCode每日一题——324. 摆动排序 II
2022-06-28 20:25:00 【hyk今天写算法了吗】
题目
给你一个整数数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]… 的顺序。
你可以假设所有输入数组都可以得到满足题目要求的结果。
示例
示例 1:
输入:nums = [1,5,1,1,6,4]
输出:[1,6,1,5,1,4]
解释:[1,4,1,5,1,6]同样是符合题目要求的结果,可以被判题程序接受。
示例 2:
输入:nums = [1,3,2,2,3,1]
输出:[2,3,1,3,1,2]
思路
题目意思是:将给定数组转换为大小数间隔出现的数组,本题即 小数—大数—小数—大数的形式。
这里可以采用穿插排序的方式:先对数组排序,分为大数部分和小数部分,再穿插排序。 注意顺序,例如[1,3,4,4,4,7]这个数组,通过降序穿插得到[4,7,3,4,1,4]。 如果顺序排列,则会得到[1,4,3,4,4,7]不满足要求。 这里是因为我们想尽量将小数部分的最大数放在边上,这样只用靠近一个大数部分的最大数。
题解
class Solution:
def wiggleSort(self, nums: List[int]) -> None:
""" Do not return anything, modify nums in-place instead. """
nums.sort()
half = len(nums[::2])
nums[::2], nums[1::2] = nums[:half][::-1], nums[half:][::-1]
边栏推荐
- MongoDB——副本集与分片
- ANR分析--问题1
- Input and output real data
- Resilience4j retry source code analysis and retry index collection
- ThreadLocal principle
- Windows 64 bit download install my SQL
- two thousand three hundred and forty-two
- Analysis of all knowledge points of TCP protocol in network planning
- 不同框架的绘制神经网络结构可视化
- 【Go语言刷题篇】Go从0到入门5:Map综合复习、条件语句、循环语句练习
猜你喜欢

2022年T电梯修理考试题库模拟考试平台操作

Day88.七牛云: 房源图片、用户头像上传

C # connect to the database to complete the operation of adding, deleting, modifying and querying

阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践

不同框架的绘制神经网络结构可视化

【Go语言刷题篇】Go从0到入门5:Map综合复习、条件语句、循环语句练习
![[go language questions] go from 0 to entry 5: comprehensive review of map, conditional sentences and circular sentences](/img/7a/16b481753d7d57f50dc8787eec8a1a.png)
[go language questions] go from 0 to entry 5: comprehensive review of map, conditional sentences and circular sentences

方 差 分 析

Leetcode 36. Effective Sudoku (yes, once)

Rsync remote synchronization
随机推荐
Various types of long
How to "calculate" in the age of computing power? The first mover advantage of "convergence of computing and networking" is very important!
Analysis of variance
稳定性总结
Win 10 create a gin framework project
The severity code indicates that the project file line prohibits the display of status errors. C1047 object or library file ".Lib" is different from other objects (such as "x64\release\main.obj")
应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)
【Try to Hack】Cobalt Strike(一)
阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践
mysql-发生系统错误1067
[learning notes] factor analysis
2022茶艺师(中级)考试模拟100题及模拟考试
2022焊工(初级)特种作业证考试题库及答案
QSP read label configuration error
实型数运算
Why does next() in iterator need to be forcibly converted?
Use of WC command
【学习笔记】主成分分析法介绍
Database learning notes (sql04)
rsync远程同步