This is the 28th day of my participation in the August Text Challenge.More challenges in August

Topic describes

This is the dynamic sum of 1480. One-dimensional arrays on LeetCode, difficulty is simple.

Tag: “simulation”, “prefix and”

Give you an array nums. RunningSum [I] = sum(nums[0]… Nums [I]).

Return the dynamic sum of NUMS.

Example 1:

Input: nums = [1, 2, 3, 4] output:,3,6,10 [1] : dynamic and calculation process of [1, 1 + 2, 1 + 2 + 3, 1 + 2 + 3 + 4].Copy the code

Example 2:

Output: input: nums =,1,1,1,1 [1] [1, 2, 3, 4, 5] : dynamic and calculation process of [1, 1 + 1, 1 + 1 + 1, 1 + 1 + 1 + 1, 1 + 1 + 1 + 1 + 1].Copy the code

Example 3:

Input: nums = [3,1,2,10,1] output: [3,4,6,16,17]Copy the code

Tip:

  • 1 <= nums.length <= 1000

  • 1 0 6 10^6
     <= nums[i] <= 
    1 0 6 10^6

One-dimensional prefix sum

Ans [I]ans[I]ans[I]ans[I]ans[I]

Code:

class Solution {
    public int[] runningSum(int[] nums) {
        int n = nums.length;
        int[] ans = new int[n];
        for (int i = 0, s = 0; i < n; i++) {
            ans[i] = s = s + nums[i];
        }
        returnans; }}Copy the code
  • Time complexity: O(n)O(n)O(n)
  • Space complexity: O(n)O(n)O(n)

Other prefixes and related content

Too easy? It is better to do a warm tree backpack 🤣

Or practice the following “prefix and” related questions:

The title Answer key The difficulty Recommend index
304. Two dimensional region and retrieval – Matrix immutable LeetCode problem solving link medium 🤩 🤩 🤩 🤩 🤩
303. Fields and retrieval – Arrays are immutable LeetCode problem solving link simple 🤩 🤩 🤩 🤩 🤩
363. The rectangular region does not exceed the maximum numerical sum of K LeetCode problem solving link difficult 🤩 🤩 🤩
523. Sum of contiguous subarrays LeetCode problem solving link medium 🤩 🤩 🤩 🤩
525. Contiguous arrays LeetCode problem solving link medium 🤩 🤩 🤩 🤩
724. Find the center index of the array LeetCode problem solving link simple 🤩 🤩 🤩 🤩 🤩
930. And the same binary subarray LeetCode problem solving link medium 🤩 🤩 🤩
1004. Maximum number of consecutive 1s III LeetCode problem solving link medium 🤩 🤩 🤩
1074. Number of elements and submatrices with target values LeetCode problem solving link difficult 🤩 🤩 🤩
1208. Make strings as equal as possible LeetCode problem solving link medium 🤩 🤩 🤩
1310. Subarray xor query LeetCode problem solving link medium 🤩 🤩 🤩 🤩
1442. Number of triples that form two xor equal arrays LeetCode problem solving link medium 🤩 🤩 🤩
1738. Find the KTH xor coordinate value LeetCode problem solving link medium 🤩 🤩 🤩
1744. Can you eat your favorite candy on your favorite day? LeetCode problem solving link medium 🤩 🤩 🤩 🤩 🤩
1749. The maximum absolute value of the sum of any subarray LeetCode problem solving link medium 🤩 🤩 🤩
1838. Frequency of the highest frequency element LeetCode problem solving link medium 🤩 🤩 🤩

Note: The above catalogues are fromwiki, any form of reprint quote please retain the source.

The last

This is the No.1480 of our “Brush through LeetCode” series of articles. The series started on 2021/01/01.

In this series of articles, in addition to explaining how to solve the problem, I will also present the most concise code possible. If a general solution is involved, the corresponding code template will also be used.

In order to facilitate the students to debug and submit the code on the computer, I set up the relevant warehouse: github.com/SharingSour…

In the repository, you’ll see links to the series of articles, the corresponding code for the series of articles, the LeetCode source links, and other preferred solutions.