LeetCode (LeetCode) Questions-Name: 1480. Dynamic sum of one-dimensional array; Type: Simple; Implementation: C++

The road is hindered and long, and the line is coming. Keep your head down and work hard, if you don't talk, you will be a blockbuster! Come on, Sao Nian!

1 topic introduction

  Original title link: 1480. Dynamic sum of one-dimensional array

  Refer to the above link for the topic, and a brief description is as follows:

Give you an array of nums. The calculation formula of the "dynamic sum" of the array is: runningSum[i] = sum(nums[0]…nums[i]).
Please return the dynamic sum of nums.

  Question example 1 is as follows:

输入:nums = [1,2,3,4]
输出:[1,3,6,10]
解释:动态和计算过程为 [1, 1+2, 1+2+3, 1+2+3+4]

  Sample question 2 is as follows:

输入:nums = [1,1,1,1,1]
输出:[1,2,3,4,5]
解释:动态和计算过程为 [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]

  Sample topic 3 is as follows:

输入:nums = [3,1,2,10,1]
输出:[3,4,6,16,17]

  It is implemented in C++, and the basic framework given by the title is as follows:

class Solution {
    
    
public:
    vector<int> runningSum(vector<int>& nums) {
    
    
		
    }
};

2 topic analysis

  First, I read the title several times carefully, and then looked at the examples given below, and found that the n-th data is equal to the (n-1) + n-th data. I’ll make a table to make it easier to understand

nums subscript 0 1 2 3
raw data 1 2 3 4
calculation process 1 1 + 2 1 + 2 + 3 1 + 2 + 3 + 4
Calculation results 1 3 6 10

  It can be seen from the above table that the calculation result of n is equal to (n-1) plus the value of n, such as 3 = 1 + 2; 6 = 3 + 3; 10 = 6 + 4;

3 Start to solve the problem

3.1 First attempt

  Code first, then analyze

class Solution {
    
    
public:
    vector<int> runningSum(vector<int>& nums) {
    
    
        
        int temp;
        vector<int> vArray;

        vArray.push_back(nums.at(0));
        temp = nums.at(0);

        for(size_t i = 1; i < nums.size(); i++){
    
    
            temp += nums.at(i);
            vArray.push_back(temp);
        }

        return vArray;
    }
};

  First, I defined a variable temp of type int, which is used as a transit; then I defined a container vArray of type int, which is used as the return value;

  The next step is to first push the first element of the array into vArray, and then use a for loop to find the sum of n-1 and n bits, then put it in temp, and then push temp into vArray;

  After the for loop is over, return to the vArray container directly;

  After testing, this method is very clumsy, but it also realizes the function. The time and memory
Insert picture description here
  consumption are as follows: the memory is a bit large, but the efficiency is not bad...

3.2 Second attempt

  This attempt is to understand their ideas after reading everyone’s solutions, and then run the practice again. The code is as follows

class Solution {
    
    
public:
    vector<int> runningSum(vector<int>& nums) {
    
    
        for(size_t i = 1; i < nums.size(); i++){
    
    
            nums[i] += nums[i - 1];
        }
        return nums;
    }
};

  In this modification, all existing variables are used instead of new local variables, which can save space. Then, for the type of variables in the for loop, I still use size_t;

  This time the code looks concise and easy to understand. For example, in this core code, the following two lines are equivalent, but it explains our previous analysis ideas. The nth is equal to the nth plus the nth -1 person;

nums[i] += nums[i - 1];

nums[i] = nums[i] + nums[i - 1];

  The result is obvious, the memory space occupied this time is small, but there is another problem, the time is long, as shown in the figure below, it is directly 8ms
Insert picture description here

  After thinking and thinking, there is only one difference from the reference solution, that is, the type of the variable is different. I defined the size_t type, and the original code uses the int type, so I have a third attempt. This time it is completely the same as the reference code Consistent.

3.3 Third attempt

  The code for this attempt is different from the second time, only the type of the variable, the specific code is as follows:

class Solution {
    
    
public:
    vector<int> runningSum(vector<int>& nums) {
    
    
        for(int i = 1; i < nums.size(); i++){
    
    
            nums[i] += nums[i - 1];
        }
        return nums;
    }
};

  I won't explain too much, just run the results directly, as shown below
Insert picture description here
  , the result of this time is a middle value, the memory is not that large, and the running time is also fast.

  The guess is that the reason should be the array. The return value types are all int types. I use a size_t type to fetch the data, and there will be time-consuming operations.

3.4 Analysis

  Why is there such a big difference in a variable? I checked and found some results, but not so detailed, as follows:

  The second one is a little clearer, that is, int is fixed at 4 bytes, but size_t is determined according to the machine operating system, etc. This is the reason for the time-consuming feeling, and the deeper reasons need to be further studied.

4 summary

  1. I had a senior recommend LeetCode (LeetCode) for me a long time ago, but I tried it at that time and found that I couldn't do anything. It was too embarrassing.
  2. It's here again. I have a certain basic knowledge to improve myself. If I don't know it, I will check the information.
  3. I feel this is very fun, I also bought Sudoku myself, I have nothing to do, exercise my brain, and my own thinking to keep myself alive;
  4. Well, it's over here, see you next time~, have a nice weekend, I wish you all the best!

If the content of the article is wrong, please comment / private message a lot of advice, thank you! If you think the content of the article is not bad, remember to click three links (like, bookmark, leave a message), your support is my greatest encouragement, thank you!

Guess you like

Origin blog.csdn.net/Fighting_Boom/article/details/106861881