-
https://leetcode.com/problems/final-value-of-variable-after-performing-operations/description/
Final Value of Variable After Performing Operations - LeetCode
Can you solve this real interview question? Final Value of Variable After Performing Operations - There is a programming language with only four operations and one variable X: * ++X and X++ increments the value of the variable X by 1. * --X and X-- decreme
leetcode.com
문제
이 프로그래밍 언어에는 4가지 연산과 하나의 변수 X가 존재한다.
- ++X와 X++은 X의 값을 1 올려주는 연산이다
- --X와 X--은 X의 값을 1 내려주는 연산이다
X의 초기값은 0이다
주어지는 문자열 배열인 operations는 연산이 담겨져 있다. 연산을 수행한 후 X의 값을 반환하여라
코드
class Solution:def finalValueAfterOperations(self, operations: List[str]) -> int:answer = 0for op in operations:if "++" in op:answer +=1else:answer -=1return answer'개발 > Leetcode' 카테고리의 다른 글
Leetcode 1678. Goal Parser Interpretation (0) 2023.07.02 Leetcode 744. Find Smallest Letter Greater Than Target (0) 2023.07.02 Leetcode 43. Multiply Strings (0) 2023.07.01 Leetcode 22. Generate Parentheses (0) 2023.07.01 Leetcode 2744. Find Maximum Number of String Pairs (0) 2023.07.01