队列和一个数组解决

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

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

import java.util.*;
public class Solution {
    //Insert one char from stringstream
    int[] isAppear = new int[256]; 
    Queue<Character> deque = new LinkedList<>();
    public void Insert(char ch)
    {
        int index = ch; //65  ‘a' 97
        if(isAppear[index] < 2){
            isAppear[index]++;
            deque.add(ch);
        }
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {
        while(!deque.isEmpty() && isAppear[deque.peek()] != 1){
            deque.poll();
        }
        return deque.isEmpty() ? '#' : deque.peek();
    }
}
全部评论

相关推荐

点赞 评论 收藏
转发
1 收藏 评论
分享
牛客网
牛客企业服务