题解 | #字符流中第一个不重复的字符#
字符流中第一个不重复的字符
http://www.nowcoder.com/practice/00de97733b8e4f97a3fb5c680ee10720
set存放当前已经存在的字符
chs存发当前唯一存在的字符队列
unique这个唯一的字符
import java.util.*;
public class Solution {
Set<Character> set = new HashSet<>();
LinkedList<Character> chs = new LinkedList<>();//当前唯一的字符
char unique = '#';
public void Insert(char ch)
{
if(set.contains(ch)){
chs.remove(new Character(ch));//删
if(ch == unique){
if(chs.size() == 0)unique = '#';
else unique = chs.getFirst();
}
} else {
if(unique == '#')unique = ch;
chs.add(ch);
}
set.add(ch);
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce()
{
return unique;
}
}