-
[LeetCode] 1480. Running Sum of 1d Array c++Coding Test/LeetCode 2022. 10. 10. 13:20728x90
- 문제
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.
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].
- 문제 해결
class Solution { public: vector<int> runningSum(vector<int>& nums) { int len = nums.size(); vector<int> answer(len, 0); answer[0] = nums[0]; for(int i = 1; i < len; i++){ answer[i] = answer[i - 1] + nums[i]; } return answer; } };
다른 풀이
vector<int> runningSum(vector<int>& nums) { int i = 1; while (i<nums.size()){ nums[i]+=nums[i-1]; i++; } return nums; }
728x90'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] 2011. Final Value of Variable After Performing Operations c++ (0) 2022.10.10 [LeetCode] 35. Search Insert Position c++ (0) 2022.10.10 [LeetCode] 1929. Concatenation of Array c++ (0) 2022.10.10 [LeetCode] 897. Increasing Order Search Tree c++ (0) 2022.09.27 [LeetCode] 617. Merge Two Binary Trees c++ (0) 2022.09.27