54.字符流中第一個不重復的字符
阿新 • • 發佈:2019-01-15
int 輸出 實現 題目 出現 array 實現一個函數 -i esc
題目描述
請實現一個函數用來找出字符流中第一個只出現一次的字符。例如,當從字符流中只讀出前兩個字符"go"時,第一個只出現一次的字符是"g"。當從該字符流中讀出前六個字符“google"時,第一個只出現一次的字符是"l"。輸出描述:
如果當前字符流沒有存在出現一次的字符,返回#字符。
題目解答
public class Solution { int[] hashtable=new int[256]; StringBuilder s=new StringBuilder(); //Insert one char from stringstream public voidInsert(char ch){ s.append(ch); if(hashtable[ch]==0){ hashtable[ch]=1; }else{ hashtable[ch]+=1; } } //return the first appearence once char in current stringstream public char FirstAppearingOnce(){ char[] str=s.toString().toCharArray();for(char c:str){ if(hashtable[c]==1){ return c; } } return ‘#‘; } }
54.字符流中第一個不重復的字符