当前位置:网站首页>Leetcode daily question - 324 Swing sort II
Leetcode daily question - 324 Swing sort II
2022-06-28 20:40:00 【Did HYK write the algorithm today】
List of articles
subject
Give you an array of integers nums, Rearrange it into nums[0] < nums[1] > nums[2] < nums[3]… The order of .
You can assume that all input arrays can get results that meet the requirements of the topic .
Example
Example 1:
Input :nums = [1,5,1,1,6,4]
Output :[1,6,1,5,1,4]
explain :[1,4,1,5,1,6] It is also the result that meets the requirements of the topic , It can be accepted by the problem determination program .
Example 2:
Input :nums = [1,3,2,2,3,1]
Output :[2,3,1,3,1,2]
Ideas
The title means : Converts the given array to an array with size number intervals , This question is decimal — Large number — decimal — Form of large numbers .
Here you can use interleaved sorting : Sort the array first , It is divided into large part and decimal part , Then interleave and sort . Order of attention , for example [1,3,4,4,4,7] This array , By interleaving in descending order [4,7,3,4,1,4]. If you arrange them in order , You will get [1,4,3,4,4,7] Not meeting the requirements . This is because we want to put the maximum number of the decimal part on the side , In this way, only the maximum number close to a large number part .
Answer key
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]
边栏推荐
- 视频号如何下载视频?来看超简单方法!
- Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
- 穩定性總結
- 2022 tea master (intermediate) examination simulated 100 questions and simulated examination
- Lucene构建索引的原理及源代码分析
- 2. integrate filter
- 输入和输出字符型数据
- 图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer
- How to understand the fast iteration of cloud native database?
- 题解 Pie(POJ3122)超详细易懂的二分入门
猜你喜欢

Pie (poj3122) super detailed and easy to understand binary introduction

【Try to Hack】Cobalt Strike(一)

APISIX 助力中东社交软件,实现本地化部署

方 差 分 析

mysql-发生系统错误1067

数据标准化处理

RT-Thread线程同步与线程通信

Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application

LeetCode每日一题——515. 在每个树行中找最大值

2022 tea master (intermediate) examination simulated 100 questions and simulated examination
随机推荐
with torch.no_grad():的使用原因
视频号如何下载视频?来看超简单方法!
理解整个网络模型的构建
不同框架的绘制神经网络结构可视化
Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
No module named ‘PyEMD‘ ;使用plt.figure()TypeError: ‘module‘ object is not callable
Leetcode 36. 有效的数独(可以,一次过)
LeetCode每日一题——515. 在每个树行中找最大值
ThreadLocal principle
Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer
ComparisonChain-文件名排序
MongoDB——副本集与分片
APISIX 助力中东社交软件,实现本地化部署
关键字long
数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
CNN-LSTM的flatten
1. integrate servlets
LeetCode每日一题——324. 摆动排序 II
Pipeline | and redirection >
LeetCode每日一题——剑指 Offer II 091. 粉刷房子