带你快速刷完67道剑指offer
这是四则或许对你有帮助的讯息
1、👉 最近我发现了一个每日都会推送最新校招资讯的《校招日程》文档,其中包括往届补录、应届实习校招信息,比如各种大厂、国企、银行、事业编等信息都会定期更新,帮忙扩散一下。
2、😍 免费分享阿秀个人学习计算机以来的收集到的免费资源,点此白嫖。
3、🚀如果你想在校招中顺利拿到更好的offer,阿秀建议你多看看前人踩过的坑和留下的经验,事实上你现在遇到的大多数问题你的学长学姐师兄师姐基本都已经遇到过了。
4、🔥 欢迎准备计算机校招的小伙伴加入我的学习圈子,一个人踽踽独行真的不如一群人报团取暖,过去22届和23届的小伙伴好好跟着走下去的,最后基本都拿到了不错的offer!如果你需要《阿秀的学习笔记》网站中📚︎校招八股文相关知识点的PDF版本的话,可以点此下载 。
# No54、字符流中第一个不重复的字符
# 题目描述
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
如果当前字符流没有存在出现一次的字符,返回#字符。
1
# 1、自己想的一种方法
class Solution
{
public:
//Insert one char from stringstream
void Insert(char ch)
{
v.push_back(ch);
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
if(v.empty()) return '#';
/*int len = v.size();*/
for (auto &ch:v) {
if (count(v.begin(), v.end(), ch) == 1) return ch;
}
return '#';
}
vector<char> v;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 2、借助一个unordered_map
这个方法要慢一些
class Solution
{
public:
//Insert one char from stringstream
void Insert(char ch)
{
v.push_back(ch);
unmp[ch]++;
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
for (auto &ch:v) {
if (unmp[ch] == 1) return ch;
}
return '#';
}
vector<char> v;
unordered_map<char, int> unmp;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 二刷:
# 1、简单的方法,复杂度稍微高一些
运行时间:4ms 占用内存:376k
class Solution
{
public:
//Insert one char from stringstream
void Insert(char ch)
{
v.push_back(ch);
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
for (auto &ch:v) {
if (count(v.begin(),v.end(),ch) == 1) return ch;
}
return '#';
}
vector<char> v;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 2、借助一个哈希表,稍微快一点了
运行时间:4ms 占用内存:376k
class Solution
{
public:
//Insert one char from stringstream
void Insert(char ch)
{
v.push_back(ch);
result[ch]++;
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
for (auto &ch:v) {
if (result[ch] == 1) return ch;
}
return '#';
}
vector<char> v;
unordered_map<char,int> result;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22