题解 | #字符流中第一个不重复的字符#
字符流中第一个不重复的字符
http://www.nowcoder.com/practice/00de97733b8e4f97a3fb5c680ee10720
利用map进行查重
import java.util.*;
public class Solution {
//Insert one char from stringstream
String str = "";
public void Insert(char ch)
{
str = str + ch;
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce()
{
Map<String,Integer> we = new HashMap<>();
String[] list = str.split("");
int op = 0;
int k = 0;
char w = '#';
for(String i : list){
if(we.containsKey(i)){
we.put(i,we.get(i)+1);
}else{
we.put(i,1);
}
}
for(int i=0;i<list.length;i++){
if(we.get(list[i]) == 1){
op = 1;
k =i;
break;
}
}
if(op == 0){
return '#';
}else{
return str.charAt(k);
}
}
}