C++ Hashmap Count


  • 0
    D

    '''
    class Solution {
    public:
    vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
    unordered_map<string, int> hash;
    vector<string> res;
    int sum;
    int num = INT_MAX;

        if (list1.empty() || list2.empty())
            return res;
        
        for (int i = 0; i < list1.size(); i++)
            hash[list1[i]] = i;
        
        for (int j = 0; j < list2.size(); j++)
        {
            if (hash.count(list2[j]))
            {
                sum = hash[list2[j]] + j;
                if (sum < num)
                {
                    num = sum;
                    res.clear();
                    res.push_back(list2[j]);
                }
                else if (sum == num)
                    res.push_back(list2[j]);
                else
                    ;
            }
        }
        return res;
    }
    

    };
    '''


Log in to reply
 

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