Hash Road solution to the first question (hash algorithm)

class Solution {

public:

//First create a hash icon to store the array

    unordered_map<int,int>visited;

public:

    vector<int> twoSum(vector<int>& nums, int target) {

//Traverse the array in nums

        for(int i=0;i<nums.size();i++)

        {

   //Access whether there is a matching value in the current hash table

            if(visited.count(target-nums[i]))

            {

//If yes, return the order of the two

                return {visited[target-nums[i]],i};

            }

//If not, store the hash value and access the next one.

            visited[nums[i]] = i;

        }

        return {};

    }

};

Guess you like

Origin blog.csdn.net/weixin_61743641/article/details/132036868