当前位置:网站首页>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]
边栏推荐
- 2. integrate filter
- 市值1200亿美金,老牌财税巨头Intuit是如何做到的?
- 03.hello_rust
- ref属性,props配置,mixin混入,插件,scoped样式
- 2022年T电梯修理考试题库模拟考试平台操作
- 【学习笔记】主成分分析法介绍
- Software supply chain security risk guide for enterprise digitalization and it executives
- [graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!
- 稳定性总结
- grep文本搜索工具
猜你喜欢

Rsync remote synchronization

Visualization of neural network structure in different frames

Bluecmsv1.6 code audit

Leetcode 36. Effective Sudoku (yes, once)
![[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

Software supply chain security risk guide for enterprise digitalization and it executives
![[learning notes] Introduction to principal component analysis](/img/24/a760d1cd095a967ef258b623eb465c.png)
[learning notes] Introduction to principal component analysis

【Try to Hack】Cobalt Strike(一)

Analysis of variance

ThreadLocal原理
随机推荐
[go language questions] go from 0 to entry 5: comprehensive review of map, conditional sentences and circular sentences
ThreadLocal principle
522. longest special sequence II (greedy & double pointer)
如何做好客户成功的底层设计|ToB大师课
数据标准化处理
openGauss内核分析之查询重写
Grep text search tool
怎么理解云原生数据库的易用性?
字符和整数
ThreadLocal原理
请允许当下国内ToB的「不完美」
数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
Day88.七牛云: 房源图片、用户头像上传
2. 整合 Filter
csdn涨薪技术-Selenium自动化测试全栈总结
Characters and integers
2022茶艺师(中级)考试模拟100题及模拟考试
Troubleshooting of pyinstaller failed to pack pikepdf
请问同业存单是否靠谱,安全吗
[learning notes] cluster analysis