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

题目描述
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。

import java.util.LinkedHashMap;
import java.util.Map;

public class Solution {
    
    private String str = new String();
    public void Insert(char ch) {
        
        str += String.valueOf(ch);
    }
    public char FirstAppearingOnce() {
        
        Map map = new LinkedHashMap();
        for(int i = 0; i < str.length(); i++) {
            
            if(map.containsKey(str.charAt(i))) {
                
                int num =map.get(str.charAt(i));
                num ++;
                map.put(str.charAt(i), num);
            }else {
                
                map.put(str.charAt(i), 1);
            }
        }
        if(map.containsValue(1)) {
            
            for(Character c : map.keySet()) {
                
                if(map.get(c) == 1){
                    
                    return c;
                }
            }
        }else
            return '#';
        return '#';
    }
}

你可能感兴趣的:(字符流中第一个不重复的字符)