剑指Offer第14题(调整数组顺序使奇数位于偶数前面)
作者:互联网
(本博客旨在个人总结回顾)
题目描述:
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半 部分,所有偶数位于数组的后半部分。
解法一:
/*
* @name ReorderOddEven
* @brief 调整数组,使得数组奇数在偶数前面
* @param [in] int * array
* @return void
*/
void ReorderOddEven(int* array, int nLength)
{
if (NULL == array || nLength <= 0)
{
return;
}
int* pLeft = array;
int* pRight = array + nLength - 1;
int tmp = 0;
while (pLeft < pRight)
{
while ((*pLeft & 0x1) != 0 && pLeft < pRight)
{
pLeft++;
}
while ((*pRight & 0x1) == 0 && pLeft < pRight)
{
pRight--;
}
if (pLeft < pRight)
{
tmp = *pLeft;
*pLeft = *pRight;
*pRight = tmp;
}
}
}
或者:
/*
* @name ReorderOddEven
* @brief 调整数组,使得数组奇数在偶数前面
* @param [in] int * array
* @return void
*/
void ReorderOddEven(int* array, int nLength)
{
if (NULL == array || nLength <= 0)
{
return;
}
int nLeft = 0;
int nRight = nLength - 1;
int tmp = 0;
while (nLeft < nRight)
{
while ((array[nLeft] & 0x1) == 1 && nLeft < nRight)
{
nLeft++;
}
while ((array[nRight] & 0x1) == 0 && nLeft < nRight)
{
nRight--;
}
if (nLeft < nRight)
{
tmp = array[nLeft];
array[nLeft] = array[nRight];
array[nRight] = tmp;
}
}
}
当面试官提出若判断条件不同时,比如整除3放前非整除3放后,或者负数放前正数放后。
考虑代码的重用性当然就是将判断封装一个函数:
解法二:
/*
* @name IsOdd
* @brief 判断整数是否为奇数
* @param [in] int value
* @return bool
*/
bool IsOdd(int value)
{
if ((value & 0x1) == 1)
{
return true;
}
return false;
}
/*
* @name IsPositive
* @brief 判断整数是否为正数
* @param [in] int value
* @return bool
*/
bool IsPositive(int value)
{
if (value >= 0)
{
return true;
}
return false;
}
/*
* @name Reorder
* @brief 调整数组,使得数组某种数在另一种数前面
* @param [in] int * array 原数组
* @param [in] int nLength 数组长度
* @param [in] bool
* @param [in] * fun 判断条件
* @param [in] int
* @return void
*/
void Reorder(int* array, int nLength, bool (*fun)(int))
{
if (NULL == array || nLength <= 0)
{
return;
}
int nLeft = 0;
int nRight = nLength - 1;
int tmp = 0;
while (nLeft < nRight)
{
while (fun(array[nLeft]) && nLeft < nRight)
{
nLeft++;
}
while (!fun(array[nRight]) && nLeft < nRight)
{
nRight--;
}
if (nLeft < nRight)
{
tmp = array[nLeft];
array[nLeft] = array[nRight];
array[nRight] = tmp;
}
}
}
测试例子:
int _tmain(int argc, _TCHAR* argv[])
{
int nLength = 0;
//测试用例:奇、偶交替 {1,2,3,4,5,6} {2,3,4,5,6}
int arr1[] = { 1, 2, 3, 4, 5, 6 };
int arr2[] = { 2, 3, 4, 5, 6 };
nLength = sizeof(arr1) / sizeof(arr1[0]);
Reorder(arr1, nLength, IsOdd);
PrintArray(arr1, nLength);
nLength = sizeof(arr2) / sizeof(arr2[0]);
Reorder(arr2, nLength, IsOdd);
PrintArray(arr2, nLength);
//测试例子:①偶数在前;{2,4,6,1,3,5}②奇数在前{1,3,5,2,4,6}
int arr3[] = { 2, 4, 6, 1, 3, 5 };
int arr4[] = { 1, 3, 5, 2, 4, 6 };
nLength = sizeof(arr3) / sizeof(arr3[0]);
Reorder(arr3, nLength, IsOdd);
PrintArray(arr3, nLength);
nLength = sizeof(arr4) / sizeof(arr4[0]);
Reorder(arr4, nLength, IsOdd);
PrintArray(arr4, nLength);
//测试例子:①传空指针;②传入数组数为1的数组;
int* arr5 = NULL;
int arr6[] = { 999 };
nLength = 0;
Reorder(arr5, nLength, IsOdd);
PrintArray(arr5, nLength);
nLength = sizeof(arr6) / sizeof(arr6[0]);
Reorder(arr6, nLength, IsOdd);
PrintArray(arr6, nLength);
system("pause");
return 0;
}
运行结果:
标签:return,14,Offer,int,array,偶数,数组,sizeof,nLength 来源: https://blog.csdn.net/qq_23903863/article/details/103235766