优化了一下,但好像又没完全优化。
import java.util.*;
public class Solution {
//Insert one char from stringstream
Map<Character,Integer> we = new HashMap<>();
Queue<Character> queue = new LinkedList<>();
public void Insert(char ch)
{
if(we.containsKey(ch)){
we.put(ch,we.get(ch)+1);
}else{
we.put(ch,1);
}
queue.offer(ch);
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce()
{
for(char i : queue){
if(we.get(i) == 1){
return i;
}
}
return '#';
}
}