当前位置:网站首页>Adjust the array order so that odd numbers precede even numbers
Adjust the array order so that odd numbers precede even numbers
2022-07-24 13:24:00 【Chen San】
Binary 1 The number of _ Niuke Tiba _ Cattle from
describe
Enter an array of integers , Implement a function to adjust the order of the Numbers in the array , Make all the odd numbers in the first half of the array , All even numbers are in the second half of the array , And guarantee odd number and odd number , The relative position between even and even numbers remains the same .
Pay attention to one more condition : Ensure that the original relative position remains unchanged , At this time, we can't exchange with each other as before , So it's better to use insert sorting at this time , But it needs to be in front of this odd number , Move even numbers backwards , Then insert this odd number .
public class Solution {
public void reOrderArray(int [] array) {
int k = 0;
for(int i = 0;i<array.length;i++){
if((array[i] & 1) == 1){
// The description is odd
int tmp = array[i];
int j = i;
while(j>k){
array[j] = array[j-1];
j--;
}
array[k++] = tmp;
}
}
}
}边栏推荐
- Sort method -- bubble sort (use an array to sort a string of numbers from large to small or from small to large)
- Relevant laws of animation movement (judge where to move according to parameters)
- 指针进阶部分(1)
- 26. Reverse linked list II
- Experience sharing | how to use SaaS for enterprise knowledge management
- About thread (3) thread synchronization
- How can the easycvr platform access special devices without authentication?
- 汉字风格迁移篇---用于汉字多字体生成的多样性正则化StarGAN
- flinksql 在yarn上面怎么 以 perjob 模式跑啊,我在sqlclient 提交任务之
- Step of product switching to domestic chips, stm32f4 switching to gd32
猜你喜欢
随机推荐
On node embedding
exception handling
Atcoder beginer contest 261 f / / tree array
Modification of EAS login interface
汉字风格迁移篇---用于汉字多字体生成的多样性正则化StarGAN
LEADTOOLS 22 套件 LEADTOOLS 超级套
爱可可AI前沿推介(7.24)
【论文阅读】Mean teachers are better role models
Experience sharing | how to use SaaS for enterprise knowledge management
23. Spiral matrix
有好用的免费的redis客户端工具推荐么?
binary search
AtCoder Beginner Contest 261E // 按位思考 + dp
AtCoder Beginner Contest 261 F // 树状数组
Step of product switching to domestic chips, stm32f4 switching to gd32
hdparm
Collection collection framework
How to configure webrtc protocol for low latency playback on easycvr platform v2.5.0 and above?
Simple use and difference of symmetric res, AES and asymmetric RSA (JWT)
Use of PageHelper









