반응형
Problem

- 리스트 안에 ++ 포함 값은 시작값 0 에 1을 더하고 -- 포함 값은 1을 빼서 최종 값을 내는 문제이다
class Solution:
def finalValueAfterOperations(self, operations: List[str]) -> int:
result = 0
for i in operations:
if "--" in i:
result -= 1
else:
result += 1
return result
그냥 딱 바로 생각나고 대부분 다 이렇게 풀었다고한다.
class Solution:
def finalValueAfterOperations(self, operations: List[str]) -> int:
A=operations.count("++X")
B=operations.count("X++")
C=operations.count("--X")
D=operations.count("X--")
return A+B-C-D
그래도 count를 써서 풀면 어떨 까하고 풀어본 방식
메모리 사용량에 있어 위의 for문 사용 방식보다 조금 더 많이 사용량이 발생한다
반응형
'Python > algorithm' 카테고리의 다른 글
| [Leetcode] 1512. Number of Good Pairs (0) | 2022.12.25 |
|---|---|
| [Leetcode] 1470. Shuffle the Array (1) | 2022.12.21 |
| [Leetcode] 480. Running Sum of 1d Array (1) | 2022.12.19 |
| [leetcode] 1108. Defanging an IP Address (2) | 2022.12.19 |
| 그룹단어체커 [22.09.04] (0) | 2022.09.04 |