现在的位置: 首页 > 综合 > 正文

3.去除字符串中重复字符,保证每个字符只出现一次

2017年11月15日 ⁄ 综合 ⁄ 共 537字 ⁄ 字号 评论关闭

//Design an algorithm and write code to remove the duplicate characters in a string 
//without using any additional buffer NOTE: One or two additional variables are fine 
//	An extra copy of the array is not
#include <iostream>
using namespace std;

//void move(char *s)
//{
//	while(*s)
//	{
//		*s =*(s+1);
//		s++;
//	}
//}
bool remove(char s[])
{
	if (s==NULL) return false;
	int n =strlen(s);
	if (n<2)return false;
	int m =0;
	for (auto i =0;i<n;i++)
	{
		int val =s[i] -'a';
		if((m &(1<<val))>0) 
		{
			for(auto j= i;s[j]!=NULL;j++)
				s[j] =s[j+1];
			i--;
		}
		m |=(1<<val);
	}
	return true;
}

void main()
{
	char s[]="aaabbb";
	remove(s);
	cout<<s;
	system("pause");
}

抱歉!评论已关闭.