JZ54

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

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

思路

使用队列和统计数组来解题,统计数组用来保存字符的出现次数

Code

import java.util.LinkedList;
import java.util.Queue;

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

相关推荐

点赞 评论 收藏
分享
就用这个吧:支持多益再加一个空气使用费
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务