JavaScript两全局数组暴力

字符流中第一个不重复的字符

http://www.nowcoder.com/questionTerminal/00de97733b8e4f97a3fb5c680ee10720

两全局数组

//Init module if you need
function Init()
{
    // write code here
    //全局声明
    arr=[]//存放只出现一次的字符
    res=''
    temp=[]//存放出现多处的字符
}
//Insert one char from stringstream
function Insert(ch)
{
    let newCh=ch
    if(ch!='/n'||ch!=null){
        this.res+=this.FirstAppearingOnce(ch)
    }else{ 
        console.log(this.res)
    } 
}
//return the first appearence once char in current stringstream
function FirstAppearingOnce(ch)
{
    // write code here

    if(this.arr.includes(ch)){
        this.temp.push(ch)
        this.arr.splice(this.arr.indexOf(ch),1);
    }else if(!(this.temp.includes(ch))){
        this.arr.push(ch);
    }
    return this.arr.length==0?'#':this.arr[0]
}
全部评论

相关推荐

Gardenia06...:刚开始学是这样的,可以看看左神和灵神都讲的不错
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务