题目描述:
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符”go”时,第一个只出现一次的字符是”g”。当从该字符流中读出前六个字符“google”时,第一个只出现一次的字符是”l”。
解题思路:
- 这道题和34.第一个只出现一次的字符位置基本上一摸一样,我们将字符串遍历放入字典中,然后遍历字典找出value值为1的便好,值得注意的是对于字典的遍历一般采用for(let [key,value] of map)来进行
解题代码:
function Init()
{
// write code here
map = new Map(); // 注意:这里不需要声明
}
//Insert one char from stringstream
function Insert(ch)
{
// write code here
if(!map.has(ch)) { // 注意:这里所给的是char
map.set(ch,1);
}else {
map.set(ch,map.get(ch) + 1);
}
}
//return the first appearence once char in current stringstream
function FirstAppearingOnce()
{
// write code here
for(let [k,v] of map) {
if(v === 1) return k;
}
return '#'
}