請實作一個函式用來找出字符流中第一個只出現一次的字符,例如,當從字符流中只讀出前兩個字符 "go" 時,第一個只出現一次的字符是 "g",當從該字符流中讀出前六個字符 “google" 時,第一個只出現一次的字符是 "l"
解題思路
這題思路很直接,用 LinkedHashMap 啪的一下就做出來了
import java.util.LinkedHashMap;
public class Solution {
private LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
//Insert one char from stringstream
public void Insert(char ch) {
if(!map.containsKey(ch)) {
map.put(ch, 1);
} else {
map.put(ch, -1);
}
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce() {
for(char ch : map.keySet()) {
if(map.get(ch) == 1) {
return ch;
}
}
return '#';
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/235927.html
標籤:其他
上一篇:資料結構學習-LeetCode21.合并兩個有序鏈表C語言
下一篇:鏈表中環的入口結點
