I start with X = 0. Each operation either increments (++X or X++) or decrements (--X or X--) the value of X by 1. So, I just iterate through the operations, adding 1 for "++" and subtracting 1 for "--".
class Solution:
def finalValueAfterOperations(self, operations: List[str]) -> int:
X = 0
for op in operations:
if '+' in op:
X += 1
else:
X -= 1
return X- Time: O(n)
- Space: O(1)