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

第二十九题 在字符串中删除指定字符

2018年04月13日 ⁄ 综合 ⁄ 共 709字 ⁄ 字号 评论关闭

题目:输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”abcd””ac”,则删除之后的第一个字符串变成”bd”

思路:用哈希表的方法来对字符进行查找,这样可以使查找的效率最高,通过2个指针来指向一个需要删除的元素,一个不需要删除的元素,然后对其进行覆盖,这样就不需要移动字符串了,提高效率

#include <iostream>
using namespace std;
void deleteword(char firststring[],char deletestring[])
{
	if (firststring==nullptr||deletestring==nullptr)
	{
		return;
	}
	const unsigned int wordsize=256;
	int Table[wordsize];
	memset(Table,0,sizeof(Table));
	const char* pTemp = deletestring;
	while (*pTemp!='\0')
	{
		Table[*pTemp]=1;
		++pTemp;
	}
	char *beg=firststring;
	char *end=firststring;
	while (*end!='\0')
	{
		if(1 != Table[*end])
		{
			*beg = *end;
			++ beg;
		}

		++end;
	}
	*beg='\0';
	cout<<firststring[0]<<endl;
	cout<<firststring[1]<<endl;
}
int main()
{
	char first[]={"abcd"};
	char deletec[]={"ab"};
	deleteword(first,deletec);
	return 0;
}

抱歉!评论已关闭.