/*
2010 年3 道百度面试题[相信,你懂其中的含金量]
1.a~z 包括大小写与0~9 组成的N 个数, 用最快的方式把其中重复的元素挑出来。
*/
void RepeatChars(char *str)
{
int tmp[256];
memset(tmp,0,sizeof(tmp));
for(int i=0;str[i]!='\0';i++)
tmp[str[i]]++;
for(int i=0;i<256;i++)
if(tmp[i]>1)
cout<<static_cast<char>(i)<<endl;
}
void RepeatCharsTest()
{
char str[]="azszz00943erdfSSJJJKKqo93";
cout<<"str"<<str<<endl;
RepeatChars(str);
}
/*
1.今年百度的一道题目
百度笔试:给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数。
要求:空间复杂度O(1),时间复杂度为O(n)。
ANSWER
Have done this.
2.百度笔试题
用C 语言实现函数void * memmove(void *dest, const void *src, size_t n)。
memmove 函数的功能是拷贝src 所指的内存内容前n 个字节到dest 所指的地址上。
*/
void * MemMove(void *dest, const void *src, size_t n)
{
if(dest==NULL||src==NULL)
return NULL;
void *res=dest;
char* pdest=(char*)dest;
char* psrc=(char*)src;
if(pdest>=psrc&&pdest<psrc+n)
{
char* pdestend=pdest+n-1;
char* psrcend=psrc+n-1;
while(n)
{
*pdestend=*psrcend;
pdestend--;psrcend--;
n--;
}
}
else
{
while(n)
{
*pdest=*psrc;
pdest++;psrc++;
n--;
}
}
return res;
}
void EvenAndOdd(int *p,int len)
{
int even=-1,odd=-1;
for(int i=0;i<len;i++)
{
if(odd==-1)
{
if(even==-1&&p[i]%2==0)
even=i;
if(even>=0&&p[i]%2!=0)
odd=i;
}
if(odd>=0&&even>=0)
{
int tmp=p[odd];
for(int k=odd;k>even;k--)
p[k]=p[k-1];
p[even]=tmp;
even++;
odd=-1;
}
}
}
void EvenAndOddTest()
{
int p[]={1,2,3,4,5,6,7,8,9,10};
int len=sizeof(p)/sizeof(int);
cout<<"the array : ";
ShowArray(p,len);
EvenAndOdd(p,len);
cout<<"the array : ";
ShowArray(p,len);
}