[Leet Code] Final Value of Variable After Performing Operations

Input: operations = ["--X","X++","X++"]
Output: 1
Explanation: The operations are performed as follows:
Initially, X = 0.
--X: X is decremented by 1, X = 0 - 1 = -1.
X++: X is incremented by 1, X = -1 + 1 = 0.
X++: X is incremented by 1, X = 0 + 1 = 1.
Input: operations = ["++X","++X","X++"]
Output: 3
Explanation: The operations are performed as follows:
Initially, X = 0.
++X: X is incremented by 1, X = 0 + 1 = 1.
++X: X is incremented by 1, X = 1 + 1 = 2.
X++: X is incremented by 1, X = 2 + 1 = 3.
Input: operations = ["X++","++X","--X","X--"]
Output: 0
Explanation: The operations are performed as follows:
Initially, X = 0.
X++: X is incremented by 1, X = 0 + 1 = 1.
++X: X is incremented by 1, X = 1 + 1 = 2.
--X: X is decremented by 1, X = 2 - 1 = 1.
X--: X is decremented by 1, X = 1 - 1 = 0.
class Solution(object):
def finalValueAfterOperations(self, operations):
"""
:type operations: List[str]
:rtype: int
"""

x = 0
for i in operations:
if "-" in i:
x -= 1
else:
x += 1
return x

--

--

--

Learning, and posting my findings!

Love podcasts or audiobooks? Learn on the go with our new app.

Top 10 FREE E-learning Authoring Tools for 2020

Lambda In Java

Introducing scalaps: Scala-inspired data structures for Python

WHY WE ALL SHOULD LEARN TO CODE

Our favorite benchmarks for testing PCs and how to use them : Gadget Game News

Readymade frontend framework, or custom? Which one is best?

Getting started with REST API Hygiene — I (Failure handling)

nextDAO Voting Result Announcement

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matthew Boyd

Matthew Boyd

Learning, and posting my findings!

More from Medium

All Possible Full Binary Trees Solution | LeetCode-894: Medium | JavaScript Implementation

Data Alignment in C++ : Part 1

What is a Queue?

LeetCode 21- Merge Two Sorted Lists