ACWING75. S and two of the numbers (the offer to prove safety)

A digital input and an array s, find the two numbers in the array, and so that they are exactly s.

If a plurality of numbers and equal to s, the output can be any pair.

You can think of the input contains at least one of each set of output condition is satisfied.

Sample
input: [1,2,3,4], sum = 7

Output: [3,4]

class Solution {
public:
    unordered_map<int,int>mp;
    vector<int> findNumbersWithSum(vector<int>& nums, int target) {
        int n = nums.size();
        for(int i = 0;i < n;i++) {
            mp[nums[i]] = 1;
            if(mp[target - nums[i]]) {
                return vector<int>{target - nums[i],nums[i]};
            }
        }
    }
};
Published 843 original articles · won praise 28 · views 40000 +

Guess you like

Origin blog.csdn.net/tomjobs/article/details/105006967