566.Find the Sum of an Array
Easy
Given an array
nums, return the sum of all its elements.
Summing is the simplest example of the accumulator patternAccumulator PatternKeeping a running result (a total, count, maximum…) that is updated once per element of a loop.: one variable carries a running result that every element updates. The same shape — initialise, update per element, return — reappears in counting, averaging, finding the maximum and many other array problems.
Example 1:
Input: nums = [3,8,-2,5]
Output: 14
Example 2:
Input: nums = [7]
Output: 7
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 1000 - ●
-1000000 ≤ nums[i] ≤ 1000000
nums =
[3, 8, -2, 5]