当前位置:网站首页>2011. 执行操作后的变量值

2011. 执行操作后的变量值

2022-06-22 00:28:00 拽拽就是我

leetcode力扣刷题打卡

题目:2011. 执行操作后的变量值
描述:存在一种仅支持 4 种操作和 1 个变量 X 的编程语言:

++X 和 X++ 使变量 X 的值 加 1
–X 和 X-- 使变量 X 的值 减 1
最初,X 的值是 0

给你一个字符串数组 operations ,这是由操作组成的一个列表,返回执行所有操作后, X 的 最终值 。

解题思路

1、按题目要求顺序编程;

原代码##

class Solution {
    
public:
    int finalValueAfterOperations(vector<string>& operations) {
    
        int x = 0;
        int len = operations.size();
        for (int i = 0; i < len; ++i) {
    
            for (int j = 0; j < 3; ++j) {
    
                if (operations[i][j] == '-') {
    
                    x--;
                    break;
                }
                if (operations[i][j] == '+') {
    
                    x++;
                    break;
                }
            }
        }
        return x;
    }
};
原网站

版权声明
本文为[拽拽就是我]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_32355021/article/details/125381942