-
Notifications
You must be signed in to change notification settings - Fork 1
/
JZ54-字符流中第一个不重复的字符.js
57 lines (52 loc) · 1.37 KB
/
JZ54-字符流中第一个不重复的字符.js
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
/*
* @Author: Ran
* @Date: 2021-07-11 12:54:26
* @LastEditTime: 2021-07-11 13:38:01
* @FilePath: \FrontEnd-Algorithm-JS\jianzhiOffer\JZ54-字符流中第一个不重复的字符.js
* @Description:
* 请实现一个函数用来找出字符流中第一个只出现一次的字符。
* 例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。
* 当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
*/
//Init module if you need
let map = []
let onceMap = []
let caseoutStr = ''
function Init() {
map = []
}
//Insert one char from stringstream
function Insert(ch) {
if (map.includes(ch) === false) {
let pos = onceMap.indexOf(ch)
if (pos === -1) {
onceMap.push(ch)
} else {
onceMap.splice(pos, 1)
map.push(ch)
}
}
}
//return the first appearence once char in current stringstream
function FirstAppearingOnce() {
if (onceMap.length === 0) {
return '#'
} else {
return onceMap[0]
}
}
let container = {};
function Init() {
container = {};
}
function Insert(ch) {
container[ch] = container[ch] ? container[ch] + 1 : 1;
}
function FirstAppearingOnce() {
for (const key in container) {
if (container[key] === 1) {
return key;
}
}
return '#';
}