题目1描述:
you are given an array of integers containing only 0s and 1s. you have to place all the 0s in even position and 1s in odd position and if suppose no if 0s exceed no. of 1s or vice versa then keep them untouched. Do that in ONE PASS and WITHOUT taking EXTRA MEMORY.
input array:
{0 1 1 0 1 0 1 0 1 1 1 0 0 1 0 1 1 }
output array:
{0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 }
题目2描述:
Given an array of red, green and blue balls arrange them in groups of all red together, greens together and blue together. Do in a single scan of the array.
This is same as You have an array containing only '0's, '1's and '2's. Club same items together in single scan.
分析:
1、题目一很容易联想到partition的作法,不过考虑到题目要求“if suppose no. of 0s exceed no. of 1s or vice versa then keep them untouched”,所以临界情况要特别注意,否则得到的结果很可能是类似于{0 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 1 }这样的结果,显然这不是题目所要求的输出。
2、维护好'0','1','2'各自的聚类指针,问题可以轻松解决
代码:
#include <iostream>
using namespace std;
template <typename T>
void PrintArr(T seq[], int n)
{
cout<<endl;
for(int i = 0; i < n; i++)
cout<<seq[i]<<"/t";
cout<<endl;
}
void RearrangeO1(int seq[],int n)
{
int index0 = 0;
int index1 = 1;
///////////////类似于partition操作
while(true)
{
while(index0 < n && seq[index0] == 0 )
index0 += 2;
if(index0 >= n)
break;
while(index1 < n && seq[index1] == 1 )
index1 += 2;
if(index1 >= n)
break;
//////////交换
int temp = seq[index0];
seq[index0] = seq[index1];
seq[index1] = temp;
}
/////////////////////////临界情况处理
////////奇数位置上已经全部为0,处理偶数位置:1尽量往前排
if(index0 >= n)
{
int indexend = (n % 2 == 0) ? n - 1 : n - 2;
while(true)
{
while(index1 < n && seq[index1] == 1)
index1 += 2;
while(indexend > index1 && seq[indexend] == 0)
indexend -= 2;
if(index1 >= indexend)
break;
/////////交换
int temp = seq[index1];
seq[index1] = seq[indexend];
seq[indexend] = temp;
}
}
////////偶数位置上已经全部为1,处理奇数位置:0尽量往前排
else
{
int indexend = (n % 2 == 0) ? n - 2 : n - 1;
while(true)
{
while(index0 < n && seq[index0] == 0)
index0 += 2;
while(indexend > index0 && seq[indexend] == 1)
indexend -= 2;
if(index0 >= indexend)
break;
/////////交换
int temp = seq[index0];
seq[index0] = seq[indexend];
seq[indexend] = temp;
}
}
}
///// e.g.
// >0 1 0 1 2 1 0<
// i0=0,i1=0,i2=6
// 0>1 0 1 2 1 0<
// i0=1,i1=1,i2=6
// 0>1>0 1 2 1 0<
// i0=1,i1=2,i2=6
// 0 0>1>1 2 1 0<
// i0=2,i1=3,i2=6
// 0 0>1 1>2 1 0<
// i0=2,i1=4,i2=6
// 0 0>1 1>0 1<2
// i0=2,i1=4,i2=5
// 0 0 0>1 1>1<2
// i0=3,i1=5,i2=5
// 0 0 0>1 1 1><2
// i0=3,i1=6,i2=5
void Rearrange012(int seq[], int n)
{
int index0 = 0;
int index1 = 0;
int index2 = n - 1;
while(index2 >= index1)
{
while(seq[index2] == 2)
index2--;
switch (seq[index1])
{
case 0 :
{
int temp = seq[index0];
seq[index0] = seq[index1];
seq[index1] = temp;
index1++;
index0++;
break;
}
case 1 :
index1++;
break;
case 2 :
{
int temp = seq[index1];
seq[index1] = seq[index2];
seq[index2] = temp;
index2--;
break;
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int arr01[] = { 0,1,1,0,1,0,1,0,1,1,1,0,0,1,0,1,1 };
cout<<"0/1重排前:";
PrintArr(arr01,17);
RearrangeO1(arr01,17);
cout<<"0/1重排后:";
PrintArr(arr01,17);
int arr012[] = { 0,2,1,0,1,1,2,0,2,0,1,0};
cout<<"0/1/2重排前:";
PrintArr(arr012,12);
Rearrange012(arr012,12);
cout<<"0/1/2重排后:";
PrintArr(arr012,12);
system("pause");
return 0;
}