Problem Solving
[Leet Code] Running Sum of 1D Array
신코기
2022. 6. 26. 23:25
Problem : https://leetcode.com/problems/running-sum-of-1d-array/
My solution :
class Solution:
def runningSum(self, A):
ans = [A[0]] * len(A)
for idx in range(1, len(A)):
ans[idx] = ans[idx - 1] + A[idx]
return ans
Submission Result :
Lesson :
1. 1D array와 2D array 크기 지정 방법이 서로 다름
# 1D Array
list_1d = ['A'] * 10
print(list_1d)
# Output
['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A']
# 2D Array
list_2d = [['A'] * 10 for _ in range(2)]
print(list_2d)
# Output
[['A', 'A', 'A'], ['A', 'A', 'A']]
2. 이번에는 방망이 깎는 노인처럼 코드 라인 수를 줄여보았다
# Before:
ans = [None] * len(A)
ans[0] = A[0]
# After:
ans = [A[0]] * len(A)