-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindFirstChar.h
More file actions
52 lines (47 loc) · 832 Bytes
/
FindFirstChar.h
File metadata and controls
52 lines (47 loc) · 832 Bytes
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
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<map>
using namespace std;
//寻找一串字符中第一个出现一次的字符;
char Find(const char * str)
{
/*
法1:map
时间复杂度:O(N*lgN)
*/
/*map<char, int> st;
for (size_t i = 0; i < strlen(str); ++i)
{
st[str[i]]++;
}
for (size_t i = 0; i < strlen(str); ++i)
{
if (st[str[i]] == 1)
return str[i];
}
return '\0';*/
/*
----------------------
直接定值法:字符共256个,建一个数组,每个字符对应位置存其出现次数;
*/
size_t chCount[256] = {0};
for (int i = 0; i < (strlen(str + 1)); ++i)
{
chCount[str[i]]++;
}
for (int i = 0; i < (strlen(str + 1)); ++i)
{
if (chCount[str[i]] == 1)
{
return str[i];
}
}
//到这没有出现一次的字符;
return '\0';
}
void TestFind()
{
char *str = "acadadcfbb";
char ch = Find(str);
cout << ch << endl;
}