2011. Final Value of Variable After Performing Operations Easy

@problem@discussion
#Array#String#Simulation



1/**
2 * [2011] Final Value of Variable After Performing Operations
3 *
4 * There is a programming language with only four operations and one variable X:
5 * 
6 * 	++X and X++ increments the value of the variable X by 1.
7 * 	--X and X-- decrements the value of the variable X by 1.
8 * 
9 * Initially, the value of X is 0.
10 * Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations.
11 *  
12 * Example 1:
13 * 
14 * Input: operations = ["--X","X++","X++"]
15 * Output: 1
16 * Explanation: The operations are performed as follows:
17 * Initially, X = 0.
18 * --X: X is decremented by 1, X =  0 - 1 = -1.
19 * X++: X is incremented by 1, X = -1 + 1 =  0.
20 * X++: X is incremented by 1, X =  0 + 1 =  1.
21 * 
22 * Example 2:
23 * 
24 * Input: operations = ["++X","++X","X++"]
25 * Output: 3
26 * Explanation: The operations are performed as follows:
27 * Initially, X = 0.
28 * ++X: X is incremented by 1, X = 0 + 1 = 1.
29 * ++X: X is incremented by 1, X = 1 + 1 = 2.
30 * X++: X is incremented by 1, X = 2 + 1 = 3.
31 * 
32 * Example 3:
33 * 
34 * Input: operations = ["X++","++X","--X","X--"]
35 * Output: 0
36 * Explanation: The operations are performed as follows:
37 * Initially, X = 0.
38 * X++: X is incremented by 1, X = 0 + 1 = 1.
39 * ++X: X is incremented by 1, X = 1 + 1 = 2.
40 * --X: X is decremented by 1, X = 2 - 1 = 1.
41 * X--: X is decremented by 1, X = 1 - 1 = 0.
42 * 
43 *  
44 * Constraints:
45 * 
46 * 	1 <= operations.length <= 100
47 * 	operations[i] will be either "++X", "X++", "--X", or "X--".
48 * 
49 */
50pub struct Solution {}
51
52// problem: https://leetcode.com/problems/final-value-of-variable-after-performing-operations/
53// discuss: https://leetcode.com/problems/final-value-of-variable-after-performing-operations/discuss/?currentPage=1&orderBy=most_votes&query=
54
55// submission codes start here
56
57impl Solution {
58    pub fn final_value_after_operations(operations: Vec<String>) -> i32 {
59        0
60    }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_2011() {
71    }
72}
73


Back
© 2025 bowen.ge All Rights Reserved.