Leetcode: 1. The sum of two numbers [Super Detailed Solution]

foreword

Some people turn on the lights to look at flowers at night, some people fall in love, some people drive at night to see the sea, and some people can't solve the first problem of leetcode.

I hope the following solutions can help you start your LeetCode problem solving journey 

topic

Given an integer array  nums and an integer target value  , please find the two integers   whose  sum is the target value target in the array   , and return their array subscripts.target

You can assume that there is only one answer for each input. However, the same element in the array cannot appear repeatedly in the answer.

You can return answers in any order.

Difficulty: Easy

Topic link: 1. The sum of two numbers

Example 1:

Input: nums = [2,7,11,15], target = 9
 Output: [0,1]
 Explanation: Because nums[0] + nums[1] == 9, return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
 Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
 Output: [0,1]

hint:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • There can only be one valid answer

topic analysis

When you see this question, please answer the question without hesitation, and directly solve the problem with violence and directly come to two layers of for loop

The first layer of loop starts from the subscript 0 of the array (that is, the first element starts to be added to the second element of the for loop of the second layer to determine whether it is equal to the target value) to find the subscripts of the two numbers After adding, it is equal to the target value. Pay attention to the size of the returned array, and return the array

code display

int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    int* arr = (int*)malloc(sizeof(int) * 2);
    int i = 0;
    int j = 0;
    for (i = 0; i < numsSize; i++)
    {
        for (j = i + 1; j < numsSize; j++)
        {
            if (nums[i] + nums[j] == target)
            {
                arr[0] = i;
                arr[1] = j;
            }
        }
    }
    *returnSize = 2;
    return arr;
}

Code super detailed analysis 

The first code is to use dynamic memory malloc to dynamically allocate space.

int* arr = (int*)malloc(sizeof(int) * 2);

Then there are two layers of for loops to solve problems violently

The first layer of for loop is based on the array subscript [0, numsSize-1] In fact, it is to traverse numsSize elements, and the value range of i is [0, numsSize-1] so that the elements of the entire array can be traversed

The loop of the second layer starts from i +1, and the range is [ i+1 , numsSize-1 ].

In this way, the first element can be added to the following elements in order to judge

For example, start from the first element: add the first element to the second element to judge whether it is equal to the target value (target), if not, let the first element add the third element to judge...until you find two suitable.

    for (i = 0; i < numsSize; i++)
    {
        for (j = i + 1; j < numsSize; j++)
        {
            if (nums[i] + nums[j] == target)
            {
                arr[0] = i;
                arr[1] = j;
            }
        }
    }

Note that the array created above should be used here to receive, so that it is convenient to return the array.

Here * returnSize = 2; must remember to write it, indicating the size of the returned array. 

Don't forget the last two lines of code

    *returnSize = 2;
    return arr;

 

Guess you like

Origin blog.csdn.net/qq_72505850/article/details/132604603