LeetCode 一维数组动态和 (swift 5.0)

class Solution {
    func runningSum(_ nums: [Int]) -> [Int] {
        if nums.count == 1 {
            return nums
        }
        
        var sum = [Int].init(repeating: nums.count, count: nums.count)
        sum[0] = nums[0]

        for (index,_) in nums.enumerated() {
            if index >= 1 {
                sum[index] = sum[index - 1] + nums[index]
            }
        }
        return sum
    }
}

传送门:LeetCode

你可能感兴趣的:(LeetCode 一维数组动态和 (swift 5.0))