当前位置:网站首页>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 .
边栏推荐
- Browser local storage
- The simple problem of leetcode is to divide an array into three parts equal to sum
- Client and server working modes of JVM
- QT (x): control operation
- Establishing the development environment of esp8266
- [Flink] flinksql and table programming cases
- Hyperledger Fabric 2. X custom smart contract
- Single application and microservice application
- Ribbon service invocation and load balancing
- Chapter V online logic analyzer signaltap
猜你喜欢

关于DDNS

Servlet version conflict causes page 404

Illustrate plug-in -- AI plug-in development -- creative plug-in -- astute graphics -- multi axis mirroring function

MySQL learning notes

Single application and microservice application

Presto-Trial

Browser local storage

Sourcetree remote red exclamation point

National Defense University project summary

配置Flutter开发环境
随机推荐
Go basic data types: characters and strings
Where is the Gcov symbol- Where are the gcov symbols?
[deep learning] - maze task learning I (to realize the random movement of agents)
Aging design guide for applets
Ribbon 服务调用与负载均衡
Fault: ntfrs warning log for id13562
Output of character pointer to string in C language
Teach you how to develop your own NPM package (publish to the NPM official website)
Installing modules in pycharm
Object detection - VIDEO reasoning using yolov6
[C language series] - branch and loop statements
Pointer from beginner to advanced (2)
'only_ full_ group_ The influence of by'sql mode on group by and its treatment
关于 localStorage 的一些高阶用法
ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience
Hyperledger Fabric 2. X custom smart contract
[high concurrency] deeply analyze the callable interface
Benign competition will promote each other
What is the "danksharding" of V God Kop on Valentine's day?
Alphacode made its debut! The programming version of "Alpha dog" competed quietly and defeated half of the programmers