1. 程式人生 > >牛客(54)字符流中第一個不重復的字符

牛客(54)字符流中第一個不重復的字符

The hashmap pan 不重復 AC else oid rst public

    Map<Character,Integer> map = new HashMap<Character, Integer>();
    ArrayList<Character> arrayList = new ArrayList<Character>();
    //Insert one char from stringstream
    public void Insert(char ch)
    {
        if (map.containsKey(ch)){
            map.put(ch,map.get(ch)
+1); }else { map.put(ch,1); } arrayList.add(ch); } //return the first appearence once char in current stringstream public char FirstAppearingOnce() { char ch=‘#‘; for (int i=0;i<arrayList.size();i++){ if (map.get(arrayList.get(i))==1){
return arrayList.get(i); } } return ch; }

牛客(54)字符流中第一個不重復的字符