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