leetcode.com/problems/running-sum-of-1d-array/
# 문제설명
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.
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].
예 1.
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].
예 1.
Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]
# 제한사항
- 1 <= nums.length <= 1000
- -10^6 <= nums[i] <= 10^6
# 풀이
class Solution {
public int[] runningSum(int[] nums) {
int[] answer = new int[nums.length];
int tmp = 0;
for(int i=0; i<nums.length; i++) {
tmp += nums[i];
answer[i] = tmp;
}
return answer;
}
}
'공부, 알고리즘 > LeetCode (리트코드)' 카테고리의 다른 글
[리트코드/JAVA] 1822. Sign of the Product of an Array (0) | 2021.04.17 |
---|---|
[리트코드/JAVA] 771. Jewels and Stones (0) | 2021.04.02 |
[리트코드/JAVA] 1470. Shuffle the Array (0) | 2021.03.23 |
[리트코드/JAVA] 1672. Richest Customer Wealth (0) | 2021.03.20 |
[리트코드/JAVA] 1431. Kids With the Greatest Number of Candies (0) | 2021.03.15 |