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

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

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

import java.util.*;
public class Solution {
    // 字符流中第一个不重复的字符
    // 第一个,要保证有序;不重复要统计数量
    ArrayList<Character> list = new ArrayList<>();
    public void Insert(char ch)
    {
        list.add(ch);
    }
    public char FirstAppearingOnce()
    {
        HashMap<Character,Integer> map = new HashMap<>();
        for(int i = 0; i < list.size(); i++){
            char c = list.get(i);
            if(map.containsKey(c)){
                map.put(c, map.get(c) + 1);
            }else{
                map.put(c,1);
            }
        }
	  	// 当全都是重复字符时,res = '#';
        char res = '#';
        for(char c : list){
            if(map.get(c) == 1){
                res = c;
                break;
            }
        }
        return res;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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