当前位置:网站首页>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
边栏推荐
- 【一起上水硕系列】最简单的字幕配置
- 對補wasm環境的一些測試
- mark
- 【無標題】
- Install mysql5.7 and change the password
- 手机开户股票开户安全吗?开户很难么?
- If you dare to write MQ message queue middleware on your resume, you must win these questions!
- PHP SimpleXML
- Sysbench Pressure Test Oracle (installation and use examples)
- 18. `bs object Node name next_ Sibling` get sibling nodes
猜你喜欢

EMC、EMI、EMS的关系

Redu.us took the initiative to transform, and the operation leader of China's charging pile emerged

蓝桥杯2022初赛——扫雷

Programmers whose monthly salary is less than 30K must recite the interview stereotype. I'll eat it first

PWN攻防世界Level2

矩阵特征值和特征向量求解——特征值分解(EVD)

Prepare for the Blue Bridge Cup - double pointer, BFS

层次分析法(AHP)
![[sans titre]](/img/36/2f9319e05157ab6a8dd5aa3bef4505.png)
[sans titre]

Oracle Recovery Tools实战批量坏块修复
随机推荐
Tortoise 没有显示绿色图标
Kubernetes: container resource requirements and constraints (constraints)
mark
【无标题】
PWN attack and defense world level2
Pytoch Learning Series: Introduction
After today, I look forward to the new year's eve of the year of the rabbit
使用gdb添加断点的几种方式
Download and installation of MySQL
Bluebridge cup 2022 preliminaries - minesweeping
Logarithmic calculation in reverse order, logarithmic calculation in sequence -- merge sort
SQL continuous login problem
微信小程序自定义组件
手机开户股票开户安全吗?开户很难么?
Some tests on complementary wasm environment
安装mysql5.7 并修改密码
LinkedList learning
LeetCode 每日一题——324. 摆动排序 II
How to optimize databases and tables
[Algèbre linéaire] 1.1 déterminant du deuxième et du troisième ordre