当前位置:网站首页>Li Kou today's question -324 Swing sort II
Li Kou today's question -324 Swing sort II
2022-06-29 06:37:00 【Struggling young man】
324. Swing sort II
Sort + Double pointer
Ideas :

class Solution {
public void wiggleSort(int[] nums) {
// take nums Clone to new array newArry
int[] newArry = nums.clone();
// take newArray Sort
Arrays.sort(newArry);
// Define the position of the two pointers
int left = (newArry.length-1)/2,right = newArry.length-1;
// Start inserting data , First insert left The element of the pointer ( Small ), In the insert right The element that the pointer points to ( Big )
for(int i = 0; i<nums.length;i++){
if(i%2 == 0){
nums[i] = newArry[left];
left--;
}else{
nums[i] = newArry[right];
right--;
}
}
}
}
One dimensional array copy mode clone
Bucket sort
We can divide all the elements into buckets and take them out one by one . Because the maximum data size is 5000, We can drive 5001 A bucket to hold elements . Then put back the original array from large to small .

Tips
Use another array as a bucket , Subscripts are used to record
value, Value is used to recordNumber of this value.
class Solution {
public void wiggleSort(int[] nums) {
// Bucket sort
//1. First define a bucket , Capacity depends on the problem , The biggest is 5000
int[] bucket = new int[5001] ;
//2. Sort the data in buckets
for(int num : nums){
//num Get is nums In the value of the ,bucket[nums] Is to take the data as a subscript , The number of occurrences of values as data
bucket[num]++;
}
// Define a variable , It will be used to get the value in the bucket later .
int j = 5000;
//3. Start to set the value , discharge 1,3,5,7..... These are all places of great value
for(int i = 1; i < nums.length ; i +=2 ){
// First find the meaningful data in the bucket
while(bucket[j] == 0){
// These are meaningless data , Quick skip , The subscript goes back
j--;
}
// Encountered data , Start assignment
nums[i] = j;
bucket[j]--;
}
//4. Reduce data ,0,2,4,6,.....
for(int i = 0 ; i < nums.length; i += 2){
while(bucket[j] == 0){
j--;
}
nums[i] = j;
bucket[j]--;
}
}
}
Suggest
Let's do it , Although it is of medium difficulty , But using the right method is not difficult .
边栏推荐
- Hyperledger Fabric 2. X custom smart contract
- Call the computer calculator and use it to convert several base numbers
- 'only_ full_ group_ The influence of by'sql mode on group by and its treatment
- Teach you how to develop your own NPM package (publish to the NPM official website)
- Overlay histogram with density curve
- Agile invincible event
- Chapter V online logic analyzer signaltap
- Are there too many programmers in China at present?
- Yyds dry goods inventory meituan's two-sided experience, and finally there was a surprise?
- Can I cast int to a variable of type byte? What happens if the value is larger than the range of byte type?
猜你喜欢

RedisTemplate处理hash整数类型的问题解析

Call the computer calculator and use it to convert several base numbers

Chapter IV introduction to FPGA development platform

Single application and microservice application

Jenkins operation Chapter 5 trigger, configuration webhook, Jenkins parameterized construction

Establishing the development environment of esp8266

2022.02.15 - 240. Lucky number in matrix
![ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience](/img/fd/4c24e10fc91a7ce7e709a0874ba675.jpg)
ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience

MySQL learning notes

Clickhouse data type
随机推荐
关于DDNS
力扣每日一题-第30天-1523.位1的个数
Print Yanghui triangle
用机器人教育创造新一代生产和服务工具
层次分析法
Character pointer as function parameter
Week 10 - task 3- from point to circle to cylinder
Installing modules in pycharm
Single application and microservice application
Segment in Lucene
The simple problem of leetcode is to divide an array into three parts equal to sum
flutter配置国内镜像,连接真机
Presto-Trial
Redistemplate handles hash integer type problem resolution
Is there any difference between a=a+b and a+=b?
[c language] [sword finger offer article] - print linked list from end to end
Overlay histogram with density curve
Illustrate plug-in -- AI plug-in development -- creative plug-in -- astute graphics -- length and angle measurement function
Leetcode simple question: judging the color of a grid on a chess board
Labor skills courses integrated into steam Education