《剑指Offer》41.2 字符流中第一个不重复的字符

题目链接

牛客网

题目描述

请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符 “go” 时,第一个只出现一次的字符是 “g”。当从该字符流中读出前六个字符“google" 时,第一个只出现一次的字符是 “l”。

解题思路

import java.util.Queue;
import java.util.LinkedList;
public class Solution {
   
    private int[] cnt = new int[256];
    private Queue<Character> q = new LinkedList<>();
    //Insert one char from stringstream
    public void Insert(char ch){
   
        cnt[ch] += 1;
        q.add(ch);
        while (!q.isEmpty() && cnt[q.peek()]>1) q.remove();
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce() {
   
        return q.isEmpty()? '#': q.peek();
    }
}
全部评论

相关推荐

前段时间投boss,实在没绷住,就发出来吧
测开小登的自我救赎:这种就别较真了,感觉应该是那种吃上了学历贬值的时代红利感觉自己也能找一堆92硕士的边角料小公司吧
点赞 评论 收藏
分享
双尔:你就写拥有ai开发经历,熟练运用提示词,优化ai,提高ai回答质量
点赞 评论 收藏
分享
迷茫的大四🐶:干脆大厂搞个收费培训得了,这样就人均大厂了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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