소프트웨어에 대한 모든 것

LeetCode 풀이 - 2011. Final Value of Variable After Performing Operations 본문

알고리즘/LeetCode

LeetCode 풀이 - 2011. Final Value of Variable After Performing Operations

앤테바 2021. 10. 16. 15:05
반응형

Final Value of Variable After Performing Operations

문제)

솔루션1)

class Solution:
    def finalValueAfterOperations(self, operations: List[str]) -> int:
        val = 0
        for operation in operations:
            if '+' in operation:
                val += 1
            else:
                val -= 1
        return val

솔루션2)

class Solution:
    def finalValueAfterOperations(self, operations: List[str]) -> int:
        return sum(1 if '+' in operation else -1 for operation in operations)

 

반응형
Comments