当前位置:网站首页>调整数组顺序使奇数位于偶数前面
调整数组顺序使奇数位于偶数前面
2022-07-21 17:46:00 【忱叁】
二进制中1的个数_牛客题霸_牛客网
描述
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
注意条件多了一个:保证原本的相对位置不变,此时我们就不能像以前那样相互交换了,那么这个时候采用插入排序比较好,但是需要在这次的奇数前面,将偶数进行向后移动,然后再把这个奇数插进去。
public class Solution {
public void reOrderArray(int [] array) {
int k = 0;
for(int i = 0;i<array.length;i++){
if((array[i] & 1) == 1){
//说明是奇数
int tmp = array[i];
int j = i;
while(j>k){
array[j] = array[j-1];
j--;
}
array[k++] = tmp;
}
}
}
}
边栏推荐
- 解决按钮UIButton重复点击问题
- OpenGL画坐标轴指示图
- 前辈的前后台分离介绍
- 浏览器类型判断
- Single cell paper records (Part17) -- integrating SC transcriptomic data across different conditions, technology and specialties
- 真人踩过的坑,告诉你避免自动化测试新手常犯的10个错误
- About the plaintext syntax of vditor Publishing
- 腾讯游戏 :我们如何基于 StarRocks 构建云原生数仓
- 关于 vditor 发布 plaintext 语法问题
- Commonly used instructions in RT thread
猜你喜欢
随机推荐
Redis学习笔记
Scheme design of meal return process of performance system
Openlayers uses canvas to draw round avatar icons
真人踩过的坑,告诉你避免自动化测试新手常犯的10个错误
ArrayList源码解析
QT configuration opencv (II): successful
"Everything is interconnected, enabling thousands of industries", the 2022 open atom global open source summit openatom openharmony sub forum is about to open
QT中配置OpenCV
Hisilicon hi3531 | Ruixin micro rk1109 realizes H264 streaming with RTSP client
基于RedisTemplate封装的工具类
编辑器公式
Single cell paper records (Part17) -- integrating SC transcriptomic data across different conditions, technology and specialties
RT-Thread中常用的指令
Token
UISwitch OFF状态下默认颜色设置
详解flex布局
was安装器启动报错
2021-09-23
华泰证券在哪开户安全,手机可以吗
Some notes of MySQL