[编程题]字符流中第一个不重复的字符
字符流中第一个不重复的字符
http://www.nowcoder.com/questionTerminal/00de97733b8e4f97a3fb5c680ee10720
import java.lang.StringBuffer;
public class Solution {
//Insert one char from stringstream
//存储字符流
StringBuffer string=new StringBuffer();
//存储字符和出现的次数,数组的索引表示字符,值表示出现的次数
//Java中的一个字符的大小为两个字节
//因为一个字符的十进制大小的范围为:0-127;
int[] charNum=new int[128];
public void Insert(char ch)
{
string.append(ch);
charNum[ch]++;
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce()
{
String str=String.valueOf(string);
//遍历字符流
for(int i=0;i<str.length();i++){
if(charNum[str.charAt(i)]==1){
return str.charAt(i);
}
}
return '#';
}
}
查看14道真题和解析