728x90
The LeetCode Beginner's Guide의 Challenge Problems 첫 문제로 나온 문제.
1480. Running Sum of 1d Array를 python로 풀기!
요약하자면, nums에 있는 요소들을 하나씩 sum해간다는 문제다.
풀이는 다음과 같다.
class Solution(object):
def runningSum(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
sum_nums = 0
r_list = []
for i, value in enumerate(nums):
sum_nums += value
r_list.append(sum_nums)
return r_list
1. int인 sum_nums와 빈 list인 r_list를 만들고,
2. value를 sum_nums에 차곡차곡 더해갔으며
3. .append()로 r_list 뒤에 하나씩 추가해갔다.
728x90
'코딩 공부 > Leetcode' 카테고리의 다른 글
[Python] 412. Fizz Buzz (0) | 2025.01.10 |
---|---|
[Python] 2381. Shifting Letters II (0) | 2025.01.09 |
[Python] 2185. Counting Words With a Given Prefix (0) | 2025.01.09 |
[Python] 1672. Richest Customer Wealth (0) | 2025.01.09 |
[Python] 3042. Count Prefix and Suffix Pairs I (0) | 2025.01.08 |