c++ two pointer


  • 0
    F

    class Solution {
    public:
    int firstUniqChar(string s) {

        if (s.size() == 0) return -1;
        
        vector<int> map(256, 0);
        int res = 0;
        
        for (int i=0; i<s.size(); ++i)
        {
            map[s[i]]++;
            if (map[s[i]] > 1 && s[i] == s[res])
                    while (res < s.size() && map[s[res]] > 1)
                        res++;
        }
        
        return res == s.size() ? -1 : res;
        
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.