공부, 알고리즘/LeetCode (리트코드)
[리트코드/JAVA] 1480. Running Sum of 1d Array
박주단
2021. 3. 9. 23:15
leetcode.com/problems/running-sum-of-1d-array/
Running Sum of 1d Array - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
# 문제설명
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;
}
}