Home LeetCode - 448. Find All Numbers Disappeared in an Array
Post
Cancel

LeetCode - 448. Find All Numbers Disappeared in an Array

448. Find All Numbers Disappeared in an Array - easy

문제

Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

Return the running sum of nums.

제한사항

  • 1 <= nums.length <= 1000
  • 10^6 <= nums[i] <= 10^6

입출력 예

1
2
3
4
5
Example :

Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].

풀이

  • Array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
    vector<int> runningSum(vector<int>& nums) {
        vector<int> res;
        int val = 0;
        
        for (auto&i : nums) {
            val += i; 
            res.push_back(val);
        }
        
        return res;
    }
};
This post is licensed under CC BY 4.0 by the author.