Leetcode Note: Go - Final Value of Variable After Performing Operations

Final Value of Variable After Performing Operations - LeetCode
https://leetcode.com/problems/final-value-of-variable-after-performing-operations/

  • Go 言語で取り組んだメモ

回答

Final Value of Variable After Performing Operations - LeetCode
https://leetcode.com/problems/final-value-of-variable-after-performing-operations/solutions/2229229/clean-simple-go-solution/

func finalValueAfterOperations(operations []string) int {
    X := 0
    
    // map + and - to values
    values := map[byte]int{'+': 1, '-': -1}
    
    for _, operation := range operations {
        // only need to check middle character
        // and mapping of the byte
        X += values[operation[1]]
    }
    
    return X
}