1 character for the first time only

1. The fact that map (key, value)

class Solution {
public:
    int FirstNotRepeatingChar(string str) {
        map<char, int> mp;
        for(int i = 0; i < str.size(); ++i)
            mp[str[i]]++;
        for(int i = 0; i < str.size(); ++i){
            if(mp[str[i]]==1)
                return i;
        }
        return -1;
    }
};

 

Guess you like

Origin www.cnblogs.com/cgy1012/p/11403128.html