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
.
Example 1:
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].
Example 2:
Input: nums = [1,1,1,1,1] Output: [1,2,3,4,5] Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
Example 3:
Input: nums = [3,1,2,10,1] Output: [3,4,6,16,17]
Constraints:
1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6
program main
! Solves the problem "Running Sum of 1d Array" from LeetCode (https://leetcode.com/problems/running-sum-of-1d-array/)
implicit none
integer, parameter :: n = 5
integer, dimension(n) :: nums
integer, dimension(n) :: runningSum
nums = [1, 2, 3, 4, 1]
runningSum = runningSumOfArray(nums)
write (*, '(A,*(I0,1X))') 'Running sum of ', nums, 'is', runningSum
contains
function runningSumOfArray(nums) result(runningSum)
implicit none
integer, intent(in) :: nums(:)
integer, dimension(size(nums)) :: runningSum
integer :: i
do i = 1, size(nums)
runningSum(i) = sum(nums(0:i-1))
end do
end function runningSumOfArray
end program main
At line 11 of file temp.f95 (unit = 6, file = 'stdout') Fortran runtime error: Expected INTEGER for item 7 in formatted transfer, got CHARACTER (A,*(I0,1X)) ^ Error termination. Backtrace: #0 0x7b58b9124960 in ??? #1 0x7b58b91254d9 in ??? #2 0x7b58b936aec3 in ??? #3 0x7b58b93780e2 in ??? #4 0x7b58b937b6d1 in ??? #5 0x7b58b937be05 in ??? #6 0x58a8ab960603 in MAIN__ #7 0x58a8ab9606df in main
```
program running_sum
implicit none
integer, parameter :: int64 = selected_int_kind(13)
integer :: i, j, n
integer(int64), allocatable :: nums(:), running_sum(:)
! read input
read(*,*) n
allocate(nums(n), running_sum(n))
do i = 1, n
read(*,*) nums(i)
end do
! compute running sum
running_sum(1) = nums(1)
do i = 2, n
running_sum(i) = running_sum(i-1) + nums(i)
end do
! print output
do i = 1, n
write(*,*) running_sum(i)
end do
end program running_sum
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:6:55: 6 | integer(int64), allocatable :: nums(:), running_sum(:) | 1 Error: Symbol ‘running_sum’ at (1) cannot have a type temp.f95:10:13: 10 | allocate(nums(n), running_sum(n)) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:12:23: 12 | read(*,*) nums(i) | 1 Error: Syntax error in READ statement at (1) temp.f95:16:16: 16 | running_sum(1) = nums(1) | 1 Error: ‘running_sum’ at (1) is not a variable temp.f95:18:20: 18 | running_sum(i) = running_sum(i-1) + nums(i) | 1 Error: ‘running_sum’ at (1) is not a variable temp.f95:23:31: 23 | write(*,*) running_sum(i) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:27:2: 27 | ``` | 1 Error: Invalid character in name at (1)
def runningSum(nums):
for i in range(1, len(nums)):
nums[i] += nums[i - 1]
return nums
The solution for this problem is to iterate through the input array nums
starting from the second element (index 1). In each iteration, we add the preceding element to the current element using the formula nums[i] += nums[i - 1]
. After finishing the loop, the modified input array is the running sum of the original array.
The logic is exactly the same across all languages; we just have to appropriately manage syntax differences.
#include <vector>
std::vector<int> runningSum(std::vector<int>& nums) {
for(int i = 1; i < nums.size(); i++) {
nums[i] += nums[i - 1];
}
return nums;
}
The solution for this problem is to iterate through the input array nums
starting from the second element (index 1). In each iteration, we add the preceding element to the current element using the formula nums[i] += nums[i - 1]
. After finishing the loop, the modified input array is the running sum of the original array.
The logic is exactly the same across all languages; we just have to appropriately manage syntax differences.