leetcode 2011. Final Value of Variable After Performing Operations(python)

文章描述了一种仅包含加减操作的编程语言,给定一个操作数组,计算变量X执行所有操作后的最终值。通过遍历操作并根据操作符号调整变量值来解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述

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.

Example 1:

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.

Example 2:

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.

Example 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.

Note:

1 <= operations.length <= 100
operations[i] will be either "++X", "X++", "--X", or "X--".

解析

根据题意,就是给出来某种编程语言的的四种操作和一个变量 X :

  • 如果操作为 ++X 或者 X++ 表示对 X 做加一操作
  • 如果操作为 --X 或者 X-- 表示对 X 做减一操作

我们初始化变量 X 为 0 ,问在执行了一系列的 operations 之后得到的最终值是多少。

其实思路很简单,就是遍历 operations 中的每一个操作 op ,如果字符串 op 中包含 - 表示的是减一操作,如果字符串 op 中包含 + 表示的是加一操作,遍历结束得到的结果就是答案。

解答

class Solution(object):
    def finalValueAfterOperations(self, operations):
        """
        :type operations: List[str]
        :rtype: int
        """
        result = 0
        for op in operations:
            if '-' in op:
                result -= 1
            elif '+' in op:
                result += 1
        return result

运行结果

Runtime: 24 ms, faster than 99.62% of Python online submissions for Final Value of Variable After Performing Operations.
Memory Usage: 13.6 MB, less than 13.41% of Python online submissions for Final Value of Variable After Performing Operations.

解析

另外我们可以用 python 的内置函数 sum ,代码简洁易懂。

sum(iterable[, start]) 函数对序列进行求和计算,传入的对象可以是列表、元组、集合等可迭代对象。例如:

>>>sum([0,1,2])  
3  

解答

class Solution(object):
    def finalValueAfterOperations(self, operations):
        """
        :type operations: List[str]
        :rtype: int
        """
        return sum(1 if '+' in o else -1 for o in operations)

运行结果

Runtime: 28 ms, faster than 99.23% of Python online submissions for Final Value of Variable After Performing Operations.
Memory Usage: 13.3 MB, less than 86.21% of Python online submissions for Final Value of Variable After Performing Operations.

原题链接:https://leetcode.com/problems/final-value-of-variable-after-performing-operations/

您的支持是我最大的动力

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王大丫丫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值