Leetcode 1480 Running Sum of 1d Array

Hello i have a question for this challange.
I saw the most submitted answer and i want to ask which is better.

  1. is most submitted

  2. is my code
    The runtime and memory usage a neraly the same.

class Solution:

    def runningSum(self, nums: List[int]) -> List[int]:

        sum = 0

        sum_arr = []

        for i in range(len(nums)):
            sum += nums[i]
            sum_arr.append(sum)

        return sum_arr
class Solution:

    def runningSum(self, nums: List[int]) -> List[int]:

        sum = 0

        sum_arr = []
      
        for i in nums:
            sum += i
            sum_arr.append(sum)

    

        return sum_arr

thank you in advance

Hi and welcome to the forum.

I think the second is a bit more Pythonic, but I wouldn’t use the variable i for that code. i should only be used for an iterator, not array elements.

Perhaps

for num in nums:
    sum += num
    # rest of function
1 Like

Okey thank you for the tip with the i as an iterator :slight_smile: