当前位置:网站首页>2011. variable value after operation

2011. variable value after operation

2022-06-22 01:34:00 Drag is me

leetcode Force button to brush questions and punch in

subject :2011. Variable value after operation
describe : There is one that only supports 4 Kind of operation and 1 A variable X Programming language :

++X and X++ Make variable X Value Add 1
–X and X-- Make variable X Value reduce 1
first ,X The value of is 0

Here's an array of strings operations , This is a list of operations , Return to after performing all operations , X Of In the end .

Their thinking

1、 Program in the order required by the topic ;

Source code ##

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;
    }
};
原网站

版权声明
本文为[Drag is me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220027200903.html