Python/algorithm

[Leetcode] 2011. Final Value of Variable After Performing Operations

baecode 2022. 12. 20. 12:56
반응형

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문 사용 방식보다 조금 더 많이 사용량이 발생한다

 

반응형