C++,unordered_map faster than map


  • 0
    B

    class Solution {
    public:
    int firstUniqChar(string s) {
    if(s.empty())
    return -1;
    unordered_map<char,int> mapTemp;
    int leng1 = s.size();
    int temp;
    for(int i = 0;i<leng1;i++)
    mapTemp[s.at(i)]++;
    for(int i = 0;i<leng1;i++)
    {
    if(mapTemp[s.at(i)] ==1)
    {
    temp = i;;
    break;
    }
    else
    temp = -1;
    }
    return temp;
    }
    };


Log in to reply
 

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