ITeye博客

字符串操作-字符串移位

zhaolei415 · 2012-08-02 14:19 · 数据结构 · 编程语言 · Markdown

人民网搜索面试---字符串移动(字符串为号和26个字母的任意组合,把号都移动到最左侧,把字母移到最右侧并保持相对顺序不变),要求时间和空间复杂度最小




#include <iostream>
#include <cstring>


using namespace std;

char* moveStar(char *s){
	if(!s) return NULL;
	int len = strlen(s);
	int starCount = 0;
	for(int i=len-1;i>=0;--i){
		if(s[i]=='*'){
			++starCount;
		}else if(starCount>0){
			 s[i+starcount] = s[i];
		}
	}
	for(int i=0;i<starCount;++i){
		s[i]='*';
	}
	return s;
}

int main()
{

	char s[7] = "AB*C*D";
	moveStar(s);
	for(int i=0;i<strlen(s);i++)
	cout<<s[i]<<endl;
    return 0;
}


评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。