当前位置:网站首页>调整数组顺序使奇数位于偶数前面(C语言)
调整数组顺序使奇数位于偶数前面(C语言)
2022-06-11 11:41:00 【Butayarou】
让我们来看一看这道题。
题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数在数组的前半部分,所有偶数在数组的后半部分。
题目来源(力扣):调整数组顺序使奇数位于偶数前面
首先我们的想法肯定是用指针,但是怎么用,又会产生出不同的解法。
方法一:不考虑相对顺序
先看第一种解法:
基本思路:
定义两个指针 left 和 right (一个在头,一个在尾),使 left 往右移,跳过奇数,直到指向的值为偶数。使right 往左移,跳过偶数, 直到指向的值为奇数。当它们都停下时,交换两个指针分别指向的值,重复上述操作,直至两个指针完成任务。
void exchange(int* nums, int numsSize)
{
int left = 0; //让left指向头
int right = numsSize - 1; //让right指向尾
while (left < right) //循环条件
{
while ((nums[left] & 1) == 1 && left < right) //left跳过奇数,直至停在偶数
{
left++;
}
while ((nums[right] & 1) == 0 && left < right) //right跳过偶数,直至停在奇数
{
right--;
}
if (left < right) //交换条件,防止交换结果不满足要求
{
int tmp = nums[left];
nums[left] = nums[right];
nums[right] = tmp;
}
}
}
这里还有一个细节,就是里面的两个while循环条件中的 left < right 都是不能不写的。
如果都不写的话,当传进去的是一个全是奇数或者全是偶数的数组时,那么会造成数组的越界访问(不一定报错)。这就告诉我们,要尽可能的考虑周全,尤其是某些特殊的测试用例。
但是上面的这种方法也有不好的地方,那就是它会把奇数间或者偶数间原有的相对顺序给打乱,那么有没有别的方法能补足这个缺陷呢?
答案是有的。
方法二:考虑相对顺序
比如下面的这种方法:
void exchange(int* nums, int numsSize)
{
int i = 0; //首先将i初始化为0
for (int j = 0; j < numsSize; j++)
{
if (nums[j] & 1 == 1) /* 进入的条件:nums[j]是奇数 */
{
int tmp = nums[j]; /* 用tmp存一下nums[j] */
for (int k = j - 1; k >= i; k--) /* 将i和j之间的偶数都向后移动一个位置 */
{
nums[k + 1] = nums[k];
}
nums[i++] = tmp; /*将 tmp 的值放进前面因移动而多出的一个空位上,再让指针 i 指向后一个位置 */
}
}
}
基本思路:
让指针 j 从头开始向后遍历,遇到奇数就停下,然后用临时变量 tmp 存一下该奇数,将指针 i (指针i前面的都是奇数)到指针 j 之间的偶数都向后移动一个位置,然后将 tmp 的值放进前面因移动而多出的一个空位上,再让指针 i 指向后一个位置。重复上面的操作,直至 j 遍历完整个数组。
可能还有别的方法,如果有兴趣,可以继续思考别的解法。
边栏推荐
- 2020-07 学习笔记整理
- 在毕设中学习03
- Runtime reconfiguration of etcd
- Memory mapping image of the grayloc module in the surfacefinder process
- Modify WordPress management account name plug-in: admin rename extended
- Etcd introduction
- Apple mobileone: the mobile terminal only needs 1ms of high-performance backbone
- Études à la fin de l'enseignement 03
- NFT digital collection system platform construction
- C# 将OFD转为PDF
猜你喜欢

CVPR 2022 | 文本引导的实体级别图像操作ManiTrans

It will be too late if you don't brush the questions. The most complete bat interview questions

Elk - elastalert largest pit

The role of Gerber file in PCB manufacturing

C# 将OFD转为PDF

MYCAT sub database and sub table

Maximum water container

Gerber文件在PCB制造中的作用

JS addition and multiplication error resolution number precision
![[JUC supplementary] atomic class, unsafe](/img/24/e51cfed39fe820fb46cca548af1782.jpg)
[JUC supplementary] atomic class, unsafe
随机推荐
软件项目管理 7.1.项目进度基本概念
Use compiler option ‘--downlevelIteration‘ to allow iterating of iterators 报错解决
Node connects to MySQL database and writes fuzzy query interface
How should ordinary people choose annuity insurance products?
ELK - X-Pack设置用户密码
Centos7.x下安装mysql8遇到的问题Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
2020-07 study notes sorting
苹果MobileOne: 移动端仅需1ms的高性能骨干
iframe 传值
Notes on topic brushing (XIV) -- binary tree: sequence traversal and DFS, BFS
WordPress database cache plug-in: DB cache Reloaded
Liufan, CFO of papaya mobile, unleashes women's innovative power in the digital age
Is the SSL certificate reliable in ensuring the information security of the website?
Hamiltonian graph
刷题笔记(十三)--二叉树:前中后序遍历(复习)
Problems encountered in installing mysql8 under centos7.x couldn't open file /etc/pki/rpm-gpg/rpm-gpg-key-mysql-2022
MSF CS OpenSSL traffic encryption
中文输入法输入事件composition的使用
在畢設中學習03
WordPress用户名修改插件:Username Changer