C++ code using unordered_set and unordered_map


  • 0

    I find hash table is really effective in some situations. My codes are below:
    ...
    int arrayNesting(vector<int>& nums)
    {
    int len=nums.size();
    int r=0,c,a;
    unordered_set<int> temp;
    unordered_map<int,int> s;
    for (int i=0;i<len;i++)
    {
    c=0;
    int j=nums[i];
    if (temp.find(j)==temp.end())
    {
    a=j;
    while(temp.find(j)==temp.end())
    {
    temp.insert(j);
    j=nums[j];
    c++;
    }
    s[a]=c+s[j];
    }
    r=max(r,s[a]);
    }
    return r;
    }
    ...


Log in to reply
 

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