当前位置:网站首页>力扣今日题-324. 摆动排序 II
力扣今日题-324. 摆动排序 II
2022-06-29 06:32:00 【抗争的小青年】
324. 摆动排序 II
排序+双指针
思路:

class Solution {
public void wiggleSort(int[] nums) {
//将nums克隆给新数组newArry
int[] newArry = nums.clone();
//将newArray进行排序
Arrays.sort(newArry);
//定义两个指针的位置
int left = (newArry.length-1)/2,right = newArry.length-1;
//开始插入数据,先插入left指针的元素(小),在插入right指针指向的元素(大)
for(int i = 0; i<nums.length;i++){
if(i%2 == 0){
nums[i] = newArry[left];
left--;
}else{
nums[i] = newArry[right];
right--;
}
}
}
}
桶排序
我们可以把所有的元素分桶然后逐个取出。由于数据大小最大是5000,我们可以开5001个桶来存放元素。然后从大到小依次放回原数组。

提示
用另外一个数组做桶,下标用来记录
值,值用来记录该值的个数。
class Solution {
public void wiggleSort(int[] nums) {
//桶排序
//1.先定义一个桶,容量看题,最大是5000
int[] bucket = new int[5001] ;
//2.将数据进行桶排序
for(int num : nums){
//num 得到的是nums 里的值,bucket[nums]就是把数据作为下标,值作为数据出现的个数
bucket[num]++;
}
//定义一个变量,待会就靠它取桶内的值了。
int j = 5000;
//3. 开始放值,放1,3,5,7.....这些都是大值待的地方
for(int i = 1; i < nums.length ; i +=2 ){
//先找到桶内有意义的数据
while(bucket[j] == 0){
//这些都是无意义的数据,快速跳过,下标往回走
j--;
}
//遇到数据了,开始赋值
nums[i] = j;
bucket[j]--;
}
//4. 放小数据,0,2,4,6,.....
for(int i = 0 ; i < nums.length; i += 2){
while(bucket[j] == 0){
j--;
}
nums[i] = j;
bucket[j]--;
}
}
}
建议
大家去做一做,虽然是中等难度的,但用对方法不难。
边栏推荐
- Top ten Devops best practices worthy of attention in 2022
- ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience
- 2022.02.14
- 5,10-di (4-aminophenyl) - 15,20-diphenylporphyrin (cis-dadph2) /5,15-di (4-aminophenyl) - 10,20-diphenylporphyrin (trans-dadph2) / (tri-apph2) supplied by Qiyue
- 5- (4-benzoimide phenyl) - 10,15,20-triphenylporphyrin (battph2) and its Zn complex (battpzn) / tetra (4-aminophenyl) porphyrin (tapph2) Qiyue supply
- Functions and arrays of shell scripts
- 2-nitro-5,10,15,20-tetra (4-methylphenyl) porphyrin copper (no2tmpp) Cu) /2-nitro-5,10,15,20-tetra (4-methylphenyl) porphyrin (no2tmpp) H2) Qiyue porphyrin supply
- 2,5-di (3,4-dicarboxyphenoxy) - 4 '- phenylethynylbiphenyldianhydride (pephqda) / Qiyue custom supply porphyrin modified amphiphilic block copolymer peg113-pcl46-porphyrin
- AIRNET notes 1
- QT (x): control operation
猜你喜欢

Hustoj SPJ example

MySQL add / delete / modify query SQL statement exercise yyds dry goods inventory

Servlet version conflict causes page 404

MySQL learning notes

Two houses with different colors and the farthest distance
![Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue](/img/a9/0869c4f39a96cff63d1e310292c46d.jpg)
Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue

Part 63 - interpreter and compiler adaptation (II)

Games101 Lecture 10 geometry 1 Notes

How to insert pseudo code into word documents simply and quickly?

Creation of Arduino uno development environment
随机推荐
ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience
Call the computer calculator and use it to convert several base numbers
Pytest (7) -yield and termination function
Hyperledger Fabric 2. X custom smart contract
After “Go to Definition”, is there a command to return to where you came from?
3 frequently tested SQL data analysis questions (including data and code)
Agile invincible event
Leetcode theme [array] -217- there are duplicate elements
Openfpga wishes you a happy Lantern Festival!
Hyperledger Fabric 2. X custom smart contract
What are the uses of wireless pressure collectors?
Case of single file component files
VLAN experiment
What is MES? What does it do?
Rich material libraries make modeling easy and efficient for developers
AIRNET notes 1
Programming specification and variables of shell script
Mongodb basic knowledge summary
Week 10 - task 0- execution process instance resolution of constructors and destructors
Ctrip launched the "3+2" office mode. Are you sour?