-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffer_50
69 lines (56 loc) · 1.27 KB
/
offer_50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//面试题50:第一个只出现一次的字符
//题目1:字符串中第一个只出现一次的字符
char FirstNotReapting(char* pString)
{
if(pString==nullptr)
return '\0';
const int tableSize=256;
unsigned int hashTable[tableSize];
for(unsigned int i=0;i<tableSize;++i){
hashTable[i]=0;
}
char* pHashKey=pString;
while(*(pHashKey)!='\0')
hash[*(pHashKey++)]++;
pHashKey=pString;
while(*pHashKey!='\0'){
if(hashTable[*pHashKey]==1)
return *pHashKey;
pHashKey++;
}
}
return '\0';
}
//题目2:字符流中第一个只出现一次的字符
class CharStatics
{
public:
CharStatics():index(0)
{
for(int i=0;i<256;++i)
occurrence[i]=-1;
}
void Insert(char ch)
{
if(occurrence[ch]=-1)
occurence[ch]=index;
else if(occurence[ch]>=0)
occurence[ch]=-2;
index++;
}
char FirstApperaringOnce()
{
char ch='\0';
int minIndex=numeric_limit<int>::max(); //表示int类型的最大值
for(int i=0;i<256;++i){
if(occurence[i]>=0 && occurence[i]<minIndex){
ch=(char)i;
minIndex=occurence[i];
}
}
return ch;
}
private:
int occurence[256];
int index;
}