LeetCode 2011: Final Value of Variable After Performing Operations (Net Delta Counting)
LeetCode 2011StringSimulationCountingToday we solve LeetCode 2011 - Final Value of Variable After Performing Operations.
Source: https://leetcode.com/problems/final-value-of-variable-after-performing-operations/
English
Problem Summary
You start with an integer variable X = 0. Each operation in the array is one of "++X", "X++", "--X", or "X--". Return the final value of X after applying all operations.
Key Insight
For the final value, pre-increment and post-increment are equivalent (both add 1), and pre-decrement and post-decrement are equivalent (both subtract 1). So each string contributes either +1 or -1.
Brute Force and Limitations
A verbose approach checks all four full strings explicitly. It works, but it is repetitive. Since each operation always contains either '+' or '-', we can classify by a single character check.
Optimal Algorithm Steps
1) Initialize x = 0.
2) Traverse every operation string op.
3) If op contains '+', do x++; otherwise do x--.
4) Return x.
Complexity Analysis
Time: O(n), where n is number of operations.
Space: O(1).
Common Pitfalls
- Overthinking prefix vs postfix in this problem (final delta is identical).
- Forgetting that every operation length is fixed and always valid.
- Writing unnecessary parsing logic.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int finalValueAfterOperations(String[] operations) {
int x = 0;
for (String op : operations) {
if (op.charAt(1) == '+') {
x++;
} else {
x--;
}
}
return x;
}
}func finalValueAfterOperations(operations []string) int {
x := 0
for _, op := range operations {
if op[1] == '+' {
x++
} else {
x--
}
}
return x
}class Solution {
public:
int finalValueAfterOperations(vector<string>& operations) {
int x = 0;
for (const string& op : operations) {
if (op[1] == '+') {
++x;
} else {
--x;
}
}
return x;
}
};from typing import List
class Solution:
def finalValueAfterOperations(self, operations: List[str]) -> int:
x = 0
for op in operations:
if op[1] == '+':
x += 1
else:
x -= 1
return xvar finalValueAfterOperations = function(operations) {
let x = 0;
for (const op of operations) {
if (op[1] === '+') {
x++;
} else {
x--;
}
}
return x;
};中文
题目概述
初始时变量 X = 0。给定操作数组,每个操作只会是 "++X"、"X++"、"--X"、"X--" 四种之一。执行完所有操作后,返回 X 的最终值。
核心思路
这题只关心最终结果,不关心表达式返回值。所以前置/后置自增效果完全一样(都 +1),前置/后置自减也一样(都 -1)。每个操作就是一个 +1 或 -1 的贡献。
朴素解法与不足
可以写四个字符串分支分别判断,但代码会重复。由于操作中间字符必然是 '+' 或 '-',直接看 op[1] 就够了。
最优算法步骤
1)初始化 x = 0。
2)遍历所有操作字符串 op。
3)若 op[1] == '+',则 x++,否则 x--。
4)返回 x。
复杂度分析
时间复杂度:O(n),其中 n 为操作数量。
空间复杂度:O(1)。
常见陷阱
- 误以为前置/后置会影响本题最终值。
- 把简单分类写成复杂解析逻辑。
- 漏掉减法分支导致结果偏大。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int finalValueAfterOperations(String[] operations) {
int x = 0;
for (String op : operations) {
if (op.charAt(1) == '+') {
x++;
} else {
x--;
}
}
return x;
}
}func finalValueAfterOperations(operations []string) int {
x := 0
for _, op := range operations {
if op[1] == '+' {
x++
} else {
x--
}
}
return x
}class Solution {
public:
int finalValueAfterOperations(vector<string>& operations) {
int x = 0;
for (const string& op : operations) {
if (op[1] == '+') {
++x;
} else {
--x;
}
}
return x;
}
};from typing import List
class Solution:
def finalValueAfterOperations(self, operations: List[str]) -> int:
x = 0
for op in operations:
if op[1] == '+':
x += 1
else:
x -= 1
return xvar finalValueAfterOperations = function(operations) {
let x = 0;
for (const op of operations) {
if (op[1] === '+') {
x++;
} else {
x--;
}
}
return x;
};
Comments