题解 | #字符流中第一个不重复的字符#

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

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

import java.util.*;
public class Solution {
    //Insert one char from stringstream
    //HashMap判断是否重复
    //队列Que给出第一个不重复的字符
    static HashMap<Character, Boolean> map = new HashMap<Character, Boolean>();
    static Queue<Character> que = new LinkedList<Character>();
    public void Insert(char ch)
    {
    //判断是否重复
    //重复,则char键对应的boolean值为false,否则为true
        if(map.containsKey(ch)){
            map.put(ch, false);
        }else {
            map.put(ch, true);
            que.offer(ch);
        }
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {
        while(!que.isEmpty()){
        //非空且que.peek()对应的值为false,poll
            if(map.get(que.peek()).equals(false)){
                que.poll();
                continue;
            }
       //非空且que.peek()对应的值为true,返回peek()
            return (char)que.peek();
        }
        //队列为空,不存在不重复的数字,返回"#"
        return '#';
    }
}
全部评论

相关推荐

纯真的河老师在喝茶:第一个是这个时间点岗位少,第二个是这个简历重复度太高了,10个有9个简历差不多的
点赞 评论 收藏
分享
notbeentak...:就抓,嗯抓,开不开匿名都要抓,一点坏事不让说,就对公司顶礼膜拜佩服的五体投地就对了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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