牛客网-剑指offer[编程题]字符流中第一个不重复的字符 js详解

牛客网-剑指offer[编程题]字符流中第一个不重复的字符 js详解_第1张图片
下面的代码:
用字母为对象的key来统计,遇到相同的字母对应的value加一,遍历输value为1的字符

//Init module if you need
let map = {
     }
function Init()
{
     
    // write code here
    map = {
     }
}
//Insert one char from stringstream
function Insert(ch)
{
     
    // write code here
    map[ch] = map[ch] ? map[ch]+1 : 1
}
//return the first appearence once char in current stringstream
function FirstAppearingOnce()
{
     
    // write code here
    for (const i in map) {
     
        if (map[i] === 1) {
     
            return i
        }
    }
    return '#'
}

你可能感兴趣的:(牛客-剑指offer)