当前位置:网站首页>力扣今日题-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]--;
}
}
}
建议
大家去做一做,虽然是中等难度的,但用对方法不难。
边栏推荐
- Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
- Design of leetcode simple problem goal parser
- What is MES? What does it do?
- Use of sed in shell script
- Pointer from beginner to advanced (2)
- QT writing map comprehensive application 58 compatible with multi browser kernel
- How to hand over complex legacy systems?
- Devops development, operation and maintenance Basics: using Jenkins to automatically build projects and notify by email
- Why can't the article be posted?
- 2,5-di (3,4-dicarboxyphenoxy) - 4 '- phenylethynylbiphenyldianhydride (pephqda) / Qiyue custom supply porphyrin modified amphiphilic block copolymer peg113-pcl46-porphyrin
猜你喜欢

Segment in Lucene

Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin

Fresnel diffraction with rectangular aperture based on MATLAB

Regular expressions for shell script values

2,5-di (3,4-dicarboxyphenoxy) - 4 '- phenylethynylbiphenyldianhydride (pephqda) / Qiyue custom supply porphyrin modified amphiphilic block copolymer peg113-pcl46-porphyrin
![[deep learning] - maze task learning I (to realize the random movement of agents)](/img/c1/95b476ec62436a35d418754e4b11dc.jpg)
[deep learning] - maze task learning I (to realize the random movement of agents)

Use of sed in shell script

2022.02.14 - 239. A single element in an ordered array

Conditional test, if and case conditional test statements of shell script

There are two ways for golang to develop mobile applications
随机推荐
Fault: KDC warning log for id29
[chromium] win10 vs2019 environment chromium configuration and compilation.
Skills of writing test cases efficiently
The first commercial spacewalk of mankind is finalized! Musk SpaceX announced a new round of space travel plan, and the American rich became repeat customers
2022.02.15
Where is the Gcov symbol- Where are the gcov symbols?
P5 DS - component and document Association
Love that can't be met -- what is the intimate relationship maintained by video chat
Stack -- 739 Daily temperature
Conditional test, if and case conditional test statements of shell script
Mongodb basic knowledge summary
Common methods for describing 3D models of objects and their advantages and disadvantages
Hyperledger Fabric 2. X custom smart contract
Review of MySQL knowledge points
Fault: administrator account cannot be selected for login
Spark saving to external data source
There are two ways for golang to develop mobile applications
Honeypot based on MySQL load data local INFILE
Hyperledger Fabric 2. X custom smart contract
Summary of redis basic knowledge points