-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path50_First_Not_Repeating.cpp
More file actions
71 lines (49 loc) · 1.52 KB
/
50_First_Not_Repeating.cpp
File metadata and controls
71 lines (49 loc) · 1.52 KB
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
65
66
67
68
69
//
// Created by mark on 2019/7/22.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:50. 第一个只出现一次的字符。
2. 思路:借助哈希表,键值时字符,值时字符出现的次数。扫描两次数组,第一次是统计字符次数,第二次是判断对应字符出现的值。
这里实现一个简单的哈希表,定义256大小的数组,根据ASCII码值作为数组的下标对应数组的一个数字;数组中存储每个字符出现的次数。
*/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <assert.h>
#include <cstdio>
#include <fstream>
#include <map>
#include <set>
using namespace std;
char FirstNotRepeating(char* pString)
{
if(pString == nullptr)
return '\0';
const int tableSize = 256;
unsigned int hashTable[tableSize]; // 创建一个数组
for(int i = 0; i < tableSize; ++i)
hashTable[i] = 0;
char* pHashKey = pString;
while(*(pHashKey) != '\0')
hashTable[*(pHashKey++)]++; // 把字符对应的次数加1,统计每个字符出现次数
pHashKey = pString;
while(*pHashKey != '\0') // 查找第一个次数为1的字符
{
if(hashTable[*pHashKey] == 1)
return *pHashKey;
pHashKey++;
}
return '\0';
}
int main()
{
char* str = "abaccdeff";
char res = FirstNotRepeating(str);
cout << res << endl;
return 0;
}