55、字符流中第一个不重复的字符
作者:
quiterr | 来源:发表于
2019-06-05 15:51 被阅读0次public class Solution {
int count[] = new int[256];
int index = 1; //可能有多个字母都只出现一次,index的作用是区分先后顺序
//Insert one char from stringstream
public void Insert(char ch)
{
if(count[ch]==0){
count[ch] = index;
index++;
}
else{
count[ch] = -1;
}
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce()
{
int index = Integer.MAX_VALUE;
char ch = '#';
for(int i=0; i<256; i++){
if(count[i]!=-1 && count[i]!=0 && count[i] < index){
index = count[i];
ch = (char)i;
}
}
return ch;
}
}
本文标题:55、字符流中第一个不重复的字符
本文链接:https://www.haomeiwen.com/subject/tjbmfxtx.html
网友评论