첫번째 코드
하나하나 따져가면서 문제를 풀면 간단하다!
"이런 경우 ~ 이렇게 함수 적용"이라는 느낌으로 if 문을 사용하면 된다.
이때 모두 문자열이므로 정수로 바꾸어 넣는 것에 유의한다.
class Solution:
def calPoints(self, operations: List[str]) -> int:
score = []
for i in operations:
if i == '+':
score.append(score[-1] + score[-2])
elif i == 'C':
score.pop()
elif i == 'D':
score.append(score[-1] * 2)
else:
score.append(int(i))
return sum(score)
통과!
링크
https://github.com/ornni/leetcode/tree/main/0682-baseball-game
leetcode/0682-baseball-game at main · ornni/leetcode
Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub v3](https://github.com/raphaelheinz/LeetHub-3.0) - ornni/leetcode
github.com
'코딩 테스트 > leetcode' 카테고리의 다른 글
Deepest Leaves Sum (0) | 2024.08.02 |
---|---|
Find Target Indices After Sorting Array (0) | 2024.07.29 |
Top K Frequent Elements (0) | 2024.07.19 |
Delete Greatest Value in Each Row (0) | 2024.07.15 |
Number of Good Pairs (0) | 2024.07.12 |