What does Hashmap[Key]++==1 mean?


  • 0
    H

    This statement turned out to be one of the key component to make the solution run faster. But what does it mean? the hashmap never got anything in them.

    std::vector<std::string> result;
    std::unordered_map<int,int> hashmap;
    
    int size=s.size();
    
    if(size>10)
    {
        for(int i =0;i<size-9;i++)
        {
            int V=0;
    
            for(int j=i;j<i+10;j++)
            {
                V <<= 3;
                V|=s[j]&7;
            }            
    
            if(hashmap[V]++==1)
            {
                result.push_back(s.substr(i,10));
            }
        }
    }
    
    return result;

  • 0
    A

    I don't know either.


  • 0
    L

    The container Map provides two ways to get an item: "operator []" and "at()", the interesting thing is that if you use operator[], eg: hashmap[V], and V is not in the keys of hashmap, this operation will insert an item to the hashmap, the key is V, the value will be "value initialized",will be the default value of the type "int", aka 0.
    so, in your case

    if(hashmap[V]++==1)
    

    is the same as

    if(hashmap[V]==1) 
    {
    // your operation
    }
    hashmap[V]++;
    

    as mentioned above, if V is not in hashmap, hashmap[V]=0. hope this helps


  • 0
    L

    and I don't think it's as important as you said ... ^_^
    it seems like some sort of syntactic sugar or a little trick ....


Log in to reply
 

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