码迷,mamicode.com
首页 > 其他好文 > 详细

找出字符串中第一个只出现一次的字符

时间:2015-04-21 22:53:16      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

find the first unique character in  a string and you can just traverse this string only one time. if there is no such character, just return ‘#‘ and ‘#‘ will not appear in the string, else return the character you find.

for example: "aAbBABac", return ‘b‘, and to "aBBa" return ‘#‘ as the final result. 

方法一:

大家熟知的,以往我们找一个字符串中第一个只出现一次的字符的时候都允许重新遍历字符串,解决的思路是建立一个hash表,hash[256],遍历一遍字符串,记录每个字符出现的次数,然后在重新遍历字符串,查看每个字符出现的次数,如果出现的次数为1,则打印出此字符,遍历停止。 然而,当只要求遍历一遍字符串的时候,只能得到每个字符出现的次数,不能得到字符原来在字符串中的排列顺序。只要能想到这一点,就能够解决问题了。再重新设定一个order数组来记录字符出现的次序,然后遍历次序数组,取出第几次出现的是哪个字符,然后在到hash表中找它出现的次数,如果次数为1,那么就找到符合要求的字符串了。代码如下:

char first_unique_word(char *s)
{
	if (strlen(s)==0)  exit(0);
	if (strlen(s)==1)  return s[0];
	const unsigned int length=strlen(s);
	vector<int> num(256,0);
	vector<int>order(256,0);
	char res='#';
	for (int i=0;i<length;i++)
	{
		if (s[i]!='/0')
		{
			num[s[i]]++;
			order[i]=s[i];//i顺序记录出现的元素
		}
	}
	for (int j=0;j<length;j++)
	{
		if (num[order[j]]==1)
		{
			res=order[j];
			break;
		}
	}
	return res;
}
方法二

方法一利用一个order数组记录顺序出现的元素,在遍历完原字符串后,还需便利一次order数组,时间复杂度为O(2n),其实在字符串中就已经记录了字符出现的先后顺序,我们可以找一种方法,不用遍历order数组,直接读取其中字符即可。因为要求的是字符串中第一次出现的唯一字符,此字符是唯一的。所以我们从后往前遍历这个字符串,最后一个新加入order的元素就是从来没有出现的且是第一次出现的。代码如下:

char first_unique_word1(char *s)
{
	if (strlen(s)==0)  exit(0);
	if (strlen(s)==1)  return s[0];
	const unsigned int length=strlen(s);
	vector<int> num(256,0);
	vector<int>order(256,0);
	char res='#';
	int j=0;
	for (int i=length-1;i>=0;i--)
	{
		if (s[i]!='/0')
		{
			num[s[i]]++;
			if (num[s[i]]==1)
			{
				order[j]=s[i];
				j++;
			}
		}
	}
	if (num[order[j-1]]==1)
	{
		res=order[j-1];
	}
	return res;

}

完整代码如下:

#include<iostream>
#include <vector>
using namespace std;
char first_unique_word(char *s);
char first_unique_word1(char *s);
void main()
{
	char s[]="abbfabbcde";
	cout<<first_unique_word(s)<<endl;
	cout<<first_unique_word1(s)<<endl;

}
char first_unique_word(char *s)
{
	if (strlen(s)==0)  exit(0);
	if (strlen(s)==1)  return s[0];
	const unsigned int length=strlen(s);
	vector<int> num(256,0);
	vector<int>order(256,0);
	char res='#';
	for (int i=0;i<length;i++)
	{
		if (s[i]!='/0')
		{
			num[s[i]]++;
			order[i]=s[i];
		}
	}
	for (int j=0;j<length;j++)
	{
		if (num[order[j]]==1)
		{
			res=order[j];
			break;
		}
	}
	return res;
}
char first_unique_word1(char *s)
{
	if (strlen(s)==0)  exit(0);
	if (strlen(s)==1)  return s[0];
	const unsigned int length=strlen(s);
	vector<int> num(256,0);
	vector<int>order(256,0);
	char res='#';
	int j=0;
	for (int i=length-1;i>=0;i--)
	{
		if (s[i]!='/0')
		{
			num[s[i]]++;
			if (num[s[i]]==1)
			{
				order[j]=s[i];
				j++;
			}
		}
	}
	if (num[order[j-1]]==1)
	{
		res=order[j-1];
	}
	return res;

}

以上代码输出为 

f

f



找出字符串中第一个只出现一次的字符

标签:

原文地址:http://blog.csdn.net/sinat_24520925/article/details/45173759

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