[LeetCode] Running Sum of 1d Array

아르당·2026년 5월 14일

LeetCode

목록 보기
309/583
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

배열 nums가 주어진다. 배열의 누적 합계를 runningSum[i] = sum(nums[0]…nums[i])로 정의한다.

nums의 누적 합계를 반환해라.

Example

#1
Input: nums = [1, 2, 3, 4]
Output: [1, 3, 6, 10]
Explanation: 누적 합계는 [1, 1 + 2, 1 + 2 + 3, 1 + 2 + 3 + 4]이다.

#2
Input: nums = [1, 1, 1, 1, 1]
Output: [1, 2, 3, 4, 5]
Explanation: 누적 합계는 [1, 1 + 1, 1 + 1 + 1, 1 + 1 + 1 + 1, 1 + 1 + 1 + 1 + 1]이다.

#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

Solved

class Solution {
    public int[] runningSum(int[] nums) {
        for(int i = 1; i < nums.length; i++){
            nums[i] = nums[i - 1] + nums[i];
        }

        return nums;
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글