[코테 풀이] Final Value of Variable After Performing Operations

시내·2024년 6월 10일
0

Q_2011) Final Value of Variable After Performing Operations

출처 : https://leetcode.com/problems/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-- decrements the value of the variable X by 1.
Initially, the value of X is 0.

Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations.

class Solution {
    public int finalValueAfterOperations(String[] operations)  {
        int res = 0;
        for(String s : operations){
            if(s.equals("--X") || s.equals("X--")){
                res--;
            }
            else{
                res++;
            }
        }
        return res;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글