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

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

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

优化了一下,但好像又没完全优化。

import java.util.*;
public class Solution {
    //Insert one char from stringstream
    Map<Character,Integer> we = new HashMap<>();
    Queue<Character> queue = new LinkedList<>();
    public void Insert(char ch)
    {
        if(we.containsKey(ch)){
            we.put(ch,we.get(ch)+1);
        }else{
            we.put(ch,1);
        }
        queue.offer(ch);
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {
        
        for(char i : queue){
            if(we.get(i) == 1){
                return i;
            }
        }
        return '#';
    }
}
全部评论

相关推荐

02-10 21:39
Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务