題目描述
請實作一個函式用來找出字符流中第一個只出現一次的字符,例如,當從字符流中只讀出前兩個字符"go"時,第一個只出現一次的字符是"g",當從該字符流中讀出前六個字符“google"時,第一個只出現一次的字符是"l",
輸出描述:
如果當前字符流沒有存在出現一次的字符,回傳#字符,
import java.util.*;
public class Solution {
private Map<Character,Integer> map = new HashMap<>();
private StringBuffer str = new StringBuffer();
//Insert one char from stringstream
public void Insert(char ch)
{
str.append(ch);
map.put(ch,map.getOrDefault(ch,0)+1);
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce()
{
for (int i = 0; i < str.length(); i++) {
if (map.get(str.charAt(i)) == 1) {
return str.charAt(i);
}
}
return '#';
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/33100.html
標籤:其他
下一篇:跟著原始碼看lcn分布式事務
