c++ array solution


  • 0
    T
    class Solution {
    public:
        int firstUniqChar(string s) {
            vector<int> indx(126, -2);
            for (int i = 0; i < s.size(); ++i) {
                indx[s[i]] = (indx[s[i]] == -2 ? i:-1);
            }
            for (int i = 0; i < s.size(); ++i) {
                if (indx[s[i]] >= 0) return i;
            }
            return -1;
        }
    };
    

Log in to reply
 

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