/*
88.2005 年11 月金山笔试题。编码完成下面的处理函数。
函数将字符串中的字符'*'移到串的前部分,前面的非'*'字符后移,
但不能改变非'*'字符的先后顺序,函数返回串中字符'*'的数量。
如原始串为:ab**cd**e*12,处理后为*****abcde12,函数并返回值为5。
*/
int MoveChar(char *str)
{
int len=strlen(str);
int slow=-1,fast=-1;
int count=0;
for(int i=0;i<=len;i++)
{
if(fast==-1)
{
if(str[i]=='*')
fast=i;
if((slow==-1)&&(str[i]!='*'))
slow=i;
}
if(slow>=0&&fast>=0)
{
count++;
char ch=str[fast];
for(int j=fast;j>slow;j--)
str[j]=str[j-1];
str[slow]=ch;
slow++;
fast=-1;
}
}
return count;
}
void MoveCharTest()
{
char str[]="ab**cd**e**f*12***";
cout<<"str : "<<str<<endl;
int count=MoveChar(str);
cout<<"after move : "<<str<<endl;
cout<<"char * : "<<count<<endl;
}
微软等数据结构+算法面试100题(13)--金山笔试题
最新推荐文章于 2018-12-28 01:05:00 发布