#The way to brush questions with leetcode 1-sum of two numbers

Keywords: C++

Title:

Given an array of integers nums and a target value target, please find the two integers with and as the target value in the array and return their array subscripts.

You can assume that each input corresponds to only one answer. However, you cannot reuse the same elements in this array.

Example:

Given nums = [2, 7, 11, 15], target =

Because nums[0] + nums[1] = 2 + 7 = 9

So return [0, 1]

 

Array at least two values

The first reaction is to use two for loops, but this is the simplest idea.

Then think about it carefully. You can add judgment before adding. The two numbers added cannot be larger than target. Now I think of it. Haha, I think it's wrong here. I didn't expect that there would be a negative number. The second number must be greater than the target first number

#include <iostream>
#include<vector>
using namespace std;

vector<int> twoSum(vector<int>& nums, int target) {
    vector<int> ans;
    int len = nums.size();
    if (len <= 1)
    {
        cout << "error" << endl;
        return ans;
    }
    for (int i = 0; i<len; i++)
    {
        int diff = target - nums[i];

        for (int j = i + 1; j<len; j++)
        {
            if (diff <= nums[j])
            {
                if (nums[i] + nums[j] == target)
                {
                    ans = { i, j };
                    return ans;
                }
            }
        }

    }
    return ans;
}


int main() {
    vector<int> nums = { -3, 4, 3, 90 };
    vector<int> ans = twoSum(nums, 0);
    std::cout << ans[0] << ans[1] << std::endl;
    return 0;
}

Note: Six methods of creating and initializing vector

29 / 29 pass test cases

Execution time: 432 ms

I'm impressed.....

Learn from experts: find that they use map to improve speed.

Note:

Amazing practical test results on Vector and Map search efficiency

Difference between Vector and Map table

A brief comparison of the efficiency of vector and map

 

Comparison of STL map and hashmap

The difference and use of map and unordered map

Method two:

vector<int> twoSum(vector<int>& nums, int target) {
    vector<int> ans;
    int len = nums.size();
    if (len <= 1)
    {
        cout << "error" << endl;
        return ans;
    }
    unordered_map<int, int> map1;
    for (int n = 0; n<len; n++){
        map1[nums[n]] = n;//key=The number in the array, value=Serial number
    }

    for (int i = 0; i<len; i++)
    {
        int diff = target - nums[i];

        if (map1.find(diff) != map1.end() && map1.find(diff)->second !=i)
        {
            ans = { i, map1.find(diff)->second };
            return ans;

        }
    }
    return ans;
}

Execution time: 20 ms comfortable

Method three:

It avoids judging whether it is the same as itself

vector<int> twoSum(vector<int>& nums, int target) {
    vector<int> ans;
    int len = nums.size();
    if (len <= 1)
    {
        cout << "error" << endl;
        return ans;
    }
    unordered_map<int, int> map1;


    for (int i = 0; i<len; map1[nums[i]] = i,i++)
    {
        int diff = target - nums[i];

        if (map1.find(diff) == map1.end() )
            continue;
        ans = { map1.find(diff)->second ,i};


    }
    return ans;
}

Execution time: 20 ms

 

This question reviews the knowledge of vector map hashmap. Remember the basic usage and operation of hash map vector, and distinguish when and what to use

Posted by Platinumweaver on Sat, 30 Nov 2019 17:10:47 -0800