剑指Offer——面试题50:第一个只出现一次的字符

面试题50:第一个只出现一次的字符

题目一:字符串中第一个只出现一次的字符。

在字符串中找出第一个只出现一次的字符。如输入"abaccdeff",则输出’b’

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int TABLE_SIZE=256;
// 利用哈希表
char FirstNotRepeatingChar(char* pString){
	if(pString==NULL) return '\0';
	unsigned int hashTable[TABLE_SIZE];
	for(unsigned int i=0;i<TABLE_SIZE;i++) 
		hashTable[i]=0;
	
	char* pHashKey=pString;
	while(*pHashKey!='\0'){
		hashTable[*(pHashKey++)]++;  // 注意 *(pHashKey++) 
	}
	pHashKey=pString;
	while(*pHashKey!='\0'){
		if(hashTable[*pHashKey]==1) return *pHashKey;
		pHashKey++;
	}
	return '\0';
}
int main() {
	printf("%c", FirstNotRepeatingChar("abaccdeff"));  
	return 0;
}

相关题目:

  • 定义一个函数,输入两个字符串,从第一个字符串中删除在第二个字符串中出现过的所有字符。例如,从第一个字符串"We are students." 中删除在第二个字符串 “aeiou” 中出现过的字符得到的结果是"W r stdnts."。
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
void deleteOneChar(char* str,char toBeDelete) {
	if (str == NULL)
		return;
	int length = strlen(str);
	char* p = str;
	for (int i=0; str[i]!='\0'&&i<length; i++) {
		if (str[i] == toBeDelete) {
			int j = i;
			for (; str[j]!='\0'&&j < length - 1; j++) {
				str[j] = str[j+1];
			}
			str[j] = '\0';
		}
	}
	return;
}

void deleteFirstInSecond(char* first,const char* second) {
	if(first==NULL)
		return;
	const int tableSize = 256;
	unsigned int hashTable[tableSize];
	for(unsigned int i = 0; i < tableSize; i++)
		hashTable[i] = 0;
	while(*(second) != '\0')
		hashTable[*(second++)]++;
	while(*(first) != '\0') {
		if (hashTable[*(first)] > 0)
			deleteOneChar(first,*(first));
		first++;
	}
}
int main() {
	char* first = "We are students";
	const char* second ="aeiou"; 
	char* p = (char*)malloc((strlen(first)+1)*sizeof(char));
	strcpy(p,first);
	deleteFirstInSecond(p,second);
	cout<<p<<endl;
	return 0;
}
  • 定义一个函数,删除字符串中所有重复出现的字符。例如,输入"google",删除重复的字符之后的结果是"gole"。
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
void deleteOneChar(char* str,char toBeDelete) {
	if (str == NULL)
		return;
	int length = strlen(str);
	char* p = str;
	for (int i=0; str[i]!='\0'&&i<length; i++) {
		if (str[i] == toBeDelete) {
			int j = i;
			for (; str[j]!='\0'&&j < length - 1; j++) {
				str[j] = str[j+1];
			}
			str[j] = '\0';
		}
	}
	return;
}

void deleteReplicaInStr(char* pStr) {
	if(pStr==NULL) return;
	const int tableSize = 256;
	unsigned int hashTable[tableSize];
	for(unsigned int i = 0; i < tableSize; i++)
		hashTable[i] = 0;
	
	while(*pStr!='\0'){
		if(hashTable[*pStr]>0){
			deleteOneChar(pStr, *pStr);
		}else{
			hashTable[*pStr]++;
			pStr++;
		}
	}
}
int main() {
	char* pStr = "google";
	char* p = (char*)malloc((strlen(pStr)+1)*sizeof(char));
	strcpy(p,pStr);
	deleteReplicaInStr(p);
	cout<<p<<endl;
	return 0;
}
  • 在英语中,如果两个单词中出现的字母相同,并且每个字母出现的次数也相同,那么这两个单词互为变位词(Anagram)。例如,silent 与 listen、evil 与 live 等互为变位词。
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
bool isAnagram(char* pStr1, char* pStr2){
	if(pStr1==NULL && pStr2==NULL) return true;
	if((pStr1==NULL && pStr2!=NULL) || (pStr1!=NULL && pStr2==NULL)) return false;
	
	const int tableSize = 256;
	unsigned int hashTable[tableSize];
	for(unsigned int i = 0; i < tableSize; i++)
		hashTable[i] = 0;
	
	while(*pStr1!='\0') hashTable[*(pStr1++)]++;
	while(*pStr2!='\0') hashTable[*(pStr2++)]--;
	for(int i=0;i<tableSize;i++){
		if(hashTable[i]!=0) return false;
	}
	return true;
} 
int main() {
	printf("%d", isAnagram("evil", "live"));
	return 0;
}

题目一:字符流中第一个只出现一次的字符。

请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是’g’。当从该字符流中读出前六个字符"google"时,第一个只出现一次的字符是’l’。

#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
char getFirstOneChar(char* pStr) {
	if(pStr==NULL) return '\0';
	const int tableSize = 256;
	unsigned int hashTable[tableSize];
	for(unsigned int i = 0; i < tableSize; i++) hashTable[i] = 0;

	char* p=pStr;
	int index=-1;
	while(*p!='\0') {
		if(hashTable[*p]>0) {
			hashTable[*p]++;
			
			if(*p==*(pStr+index)){
				int i=0;
				for(i=index;i<p-pStr;i++){
					if(hashTable[*(pStr+i)]==1) {
						index=i;
						break;
					}
				}
				if(i==p-pStr) index=-1;
			}
			p++;
		} else {
			if(index==-1) index=p-pStr;
			hashTable[*(p++)]++;
		}
	}
	return *(pStr+index);
}
int main() {
	printf("%c", getFirstOneChar("googlel"));
	return 0;
}
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
class CharStatistics{
	public:
		CharStatistics():index(0){
			for(int i=0;i<256;i++) occurrence[i]=-1;
		}
		
		void Insert(char ch){
			if(occurrence[ch]==-1) occurrence[ch]=index;
			else if(occurrence[ch]>=0) occurrence[ch]=-2;
			
			index++;
		}
		
		char FirstAppearingOnce(){
			char ch='\0';
			int minIndex=numeric_limits<int>::max();
			for(int i=0;i<256;i++){
				if(occurrence[i]>=0 && occurrence[i]<minIndex){
					ch=(char)i;
					minIndex=occurrence[i];
				}
			}
			return ch;
		}
	private:
		// occurrence[i]:A character with ASCII value i;
		// occurrence[i]=-1:The character has not found;
		// occurrence[i]=-2:The character has been found for multple times;
		// occurrence[i]>=0:The character has been found only once;
		int occurrence[256];
		int index;
};
int main() {
	CharStatistics chars;
	chars.Insert('g');
	chars.Insert('o');
	chars.Insert('o');
	chars.Insert('g');
	chars.Insert('l');
	chars.Insert('e');
	chars.Insert('l');
	printf("%c", chars.FirstAppearingOnce());
	return 0;
}
发布了53 篇原创文章 · 获赞 54 · 访问量 2752

猜你喜欢

转载自blog.csdn.net/qq_35340189/article/details/104461191