본문 바로가기
IT/알고리즘

73] Leetcode 2011. Final Value of Variable After Performing OperationsEasy1.1K151 #Kotlin

by 깻잎쌈 2023. 4. 5.
반응형

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

 

 주어진 문자열에 맞춰 연산하는 문제.

class Solution {
    fun finalValueAfterOperations(operations: Array<String>): Int {

        var ans = 0;
        for(i in 0 until operations.size){
            if(operations[i].contains("--")) {
                ans--;
            } else{
                ans++;
            }
        }

        return ans;
        
    }
}
반응형

댓글