LeetCode problem#1480
Running Sum of 1d Array
Solved in 2 languagesGoJava
Solution
11 lines
1func runningSum(nums []int) []int {
2 var sum int
3 var runningSum []int = make([]int, len(nums))
4
5 for i := 0; i < len(nums); i++ {
6 sum = sum + nums[i]
7 runningSum[i] = sum
8 }
9
10 return runningSum
11}1class Solution {
2 public int[] runningSum(int[] nums) {
3 int sum = 0;
4 int[] runningSum = new int[nums.length];
5
6 for (int i = 0; i < nums.length; i++)
7 {
8 sum = sum + nums[i];
9 runningSum[i] = sum;
10 }
11
12 return runningSum;
13 }
14}Want a walkthrough of this Go solution?
Get a step-by-step explanation of the idea behind the code.
Want a walkthrough of this Java solution?
Get a step-by-step explanation of the idea behind the code.
Problem statements belong to LeetCode and are on leetcode.com. We're not affiliated with LeetCode.