剑指offer 字符流中第一个不重复的字符

it2022-05-05  155

开一个 hash 表 就可以了

请实现一个函数用来找出字符流中第一个只出现一次的字符。 例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。 当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。 import java.util.*; public class Solution { Map<Character,Integer> map = new LinkedHashMap<>(); public void Insert(char ch) { map.put(ch,map.getOrDefault(ch, 0) + 1); } public char FirstAppearingOnce() { for(Map.Entry<Character, Integer> entry : map.entrySet()) { if(entry.getValue() == 1) { return entry.getKey(); } } return '#'; } }

 


最新回复(0)