리트코드 풀이

Leetcode 2011 풀이(파이썬, 스위프트)

ag2개발자 2022. 2. 4. 19:24

++가 들어있으면 a라는 변수를 만들어서 +=1 해주고 아니면 -=1 해주는 단순한 문제

스위프트:

class Solution {
    func finalValueAfterOperations(_ operations: [String]) -> Int {
        var a = 0
        for i in operations{
            if i.contains("++"){
                a+=1
            }
            else{
                a-=1
            }
        }
        return a
    }
}

파이썬:

class Solution:
    def finalValueAfterOperations(self, operations: List[str]) -> int:
        x=0
        for i in range(len(operations)):
            if "++" in operations[i]:
                x+=1
            else:
                x-=1
        return x