LoginSignup
0
0

LeetCode 1480. Running Sum of 1d Array

Posted at

1480. Running Sum of 1d Array

リストが与えられていて、返り値もリストで返す。
返すリストのi番目の要素は、与えられているリストのi番目以前の要素の和

class Solution:
    def runningSum(self, nums: List[int]) -> List[int]:
        num_list = []
        for i in range(1,len(nums)+1):
            num_list.append(sum(nums[0:i]))
        return num_list

他の方の回答を見て、sum使わなくてもできることに気付いた。

num_list[i] = num_list[i-1]

という感じにできるよなぁと

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0