54,字符流中第一个不重复的字符
字符流中第一个不重复的字符
https://www.nowcoder.com/questionTerminal/00de97733b8e4f97a3fb5c680ee10720?answerType=1&f=discussion
也算是一种思路,使用HashSet存储不重复字符,每个字符只会存一次,不重复存,使用ArrayList存第一次出现的字符,只会存出现一次的,重复出现就删除。
import java.util.ArrayList; import java.util.HashSet; public class Solution { HashSet<Character> set = new HashSet<>(); ArrayList<Character> list = new ArrayList<>(); //Insert one char from stringstream public void Insert(char ch) { if (set.contains(ch)){ for (int i = 0;i<list.size(); i++){ if (ch == list.get(i)){ list.remove(i); } } }else { set.add(ch); list.add(ch); } } //return the first appearence once char in current stringstream public char FirstAppearingOnce() { if (!list.isEmpty()) return list.get(0); return '#'; } }