当前位置:网站首页>Leetcode daily question - 324 Swing sort II
Leetcode daily question - 324 Swing sort II
2022-06-29 02:52:00 【SK_ Jaco】
1. Title Description
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 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]
2. Problem solving ideas and codes
2.1 Their thinking
This problem needs to arrange the original array into nums[0] < nums[1] > nums[2] < nums[3] Such a large and small structure , Then we can sort the array from small to large , And cut it in the middle , The left part is smaller , The right part is larger , Then put back the array in the order of small and large , Then we can get the result . In array nums = [1,5,1,1,6,4] For example
Use left Point to the far right of the smaller part ,right Point to most of the left and right sides , use getSmaller To decide whether to choose a smaller number or a larger number 

2.2 Code
class Solution {
public void wiggleSort(int[] nums) {
// Use a temporary array to hold
int[] tmp = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
tmp[i] = nums[i];
}
// Sort the array
Arrays.sort(tmp);
// left Start at the far right of the smaller area
int left = (nums.length - 1) >> 1;
// right Start at the far right of the larger area
int right = nums.length - 1;
// Whether to select a smaller area
boolean getSmaller = true;
for (int i = 0; i < nums.length; i++) {
if (getSmaller) {
// Select smaller area ,left The left one , and getSmaller Turn into false Take a larger area next time
nums[i] = tmp[left--];
getSmaller = false;
} else {
// Select a larger area ,right The left one , and getSmaller Turn into true Take a smaller area next time
nums[i] = tmp[right--];
getSmaller = true;
}
}
}
}
2.3 test result
Pass the test

3. summary
- First, sort the array , And then it is divided into a smaller number area and a larger number area
- Use double pointers to select smaller and larger areas and place them in the array
边栏推荐
- On the fact that lambda expressions cannot handle recursion
- Exec function of PHP
- PWN新手入门Level0
- Install mysql5.7 and change the password
- LinkedList learning
- 数据离散化
- fsockopen函数的应用
- 信息学奥赛一本通 1361:产生数(Produce)
- PAT甲级 A1057 Stack
- Three methods of time series prediction: statistical model, machine learning and recurrent neural network
猜你喜欢

Introduction to openresty

Download and installation of MySQL

Overview of PMP project management

认证培训|StreamNative Certification 培训第2期

Lanbao sensor technology rushes to the scientific innovation board: annual revenue of 350million yuan xuyongtong family has a strong color
![[Algèbre linéaire] 1.1 déterminant du deuxième et du troisième ordre](/img/ea/70b59c64d3287a887e371a9181fe45.png)
[Algèbre linéaire] 1.1 déterminant du deuxième et du troisième ordre

mgalcu-a509

逆序对对数计算,顺序对对数计算——归并排序
![[together with Shangshui Shuo series] day 6-strong liver academic paper! The most detailed explanation!](/img/70/595a94ba19d29a56a4f0bb5964a199.png)
[together with Shangshui Shuo series] day 6-strong liver academic paper! The most detailed explanation!

EMC、EMI、EMS的关系
随机推荐
PAT甲级 A1057 Stack
【一起上水硕系列】最简单的字幕配置
PWN beginner level0
PWN攻防世界Level2
Pvcreate ASM disk causes abnormal recovery of ASM disk group - sparing separation
Matrix eigenvalue and eigenvector solution - eigenvalue decomposition (EVD)
【无标题】
PHP system function
They all talk about interviews with big factories. When I interview with small factories, I invite people to drink tea?
矩阵特征值和特征向量求解——特征值分解(EVD)
After today, I look forward to the new year's eve of the year of the rabbit
【一起上水硕系列】Day 6-强肝学术论文!最细解释!
allegro对走好的线取消走线的方法
In the name of love, fresh e-commerce companies rush to sell flowers on Valentine's Day
There's a mystery behind the little login
Oracle Recovery Tools实战批量坏块修复
方法重载小结
PWN attack and defense world level2
安装mysql5.7 并修改密码
今日直播|Apache Pulsar x KubeSphere 在线 Meetup 火热来袭