字符流中第一个只出现一次的字符

class Solution{
public:
    string str;
    int i=0;
    int cnt[256]={0};
    //Insert one char from stringstream
    void insert(char c){
        str+=c;
        cnt[c]++;
    }
    //return the first appearence once char in current stringstream
    char firstAppearingOnce(){
        while(i<str.size())
        {
            if(cnt[str[i]]==1)  return str[i];
            i++;
        }
        return '#';
    }
};
posted @ 2023-04-27 15:00  穿过雾的阴霾  阅读(23)  评论(0)    收藏  举报