码迷,mamicode.com
首页 > 编程语言 > 详细

剑指offer统计字符数组中第一次出现的字符

时间:2015-04-03 13:34:49      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

给定一个字符串,例如“abaccdeff”则第一次出现的字符就是b;

#include<iostream>
#include<string>
using namespace std;
void first(const string &input)
{
	if(input.empty())
		return ;
	string::size_type length=input.size();
	int *times=new int[length];
	for(int i=0;i<length;++i)
		times[i]=0;
	string temp;
	int index=0;
	for(int j=0;j<length;++j)
	{
		string::size_type pos=0;
		char  key=input[j];
		pos=temp.find(key);
		if(pos==string::npos)
		{
			temp.push_back(key);
			times[index]++;
			index++;
		}
		else
		{
			times[pos]++;
		}

	}
	for(int i=0;i<temp.size();++i)
	{
		if(times[i]==1)
		{
			cout<<"the first char is"<<temp[i]<<endl;
			return ;
		}
	}
}
int main()
{
	string input("abaccdeff");
	first(input);
	system("pause");
	return 0;
}



剑指offer统计字符数组中第一次出现的字符

标签:

原文地址:http://blog.csdn.net/qq_22335577/article/details/44852825

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!