🤖

100日アルゴリズム[1日目・配列]

2024/03/18に公開

解いた問題

https://leetcode.com/problems/running-sum-of-1d-array/

solution

function runningSum(nums: number[]): number[] {
    let runningSum:number[] = [];
    for(let i:number = 0; i < nums.length; i++){
        if(runningSum[i-1]== undefined) {
            const theLastSum:number = nums[i];
            runningSum.push(theLastSum);
        } else {
        const theLastSum:number = runningSum[i-1] + nums[i];
        runningSum.push(theLastSum);
        }
    }
    return runningSum;
};

ポイント

runningSumの部分でnumberを指定してなかったらneverが出た。細かいけど注意

Discussion