|
| 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 | + */ |
| 50 | +pub 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 | + |
| 57 | +impl Solution { |
| 58 | + pub fn final_value_after_operations(operations: Vec<String>) -> i32 { |
| 59 | + operations |
| 60 | + .iter() |
| 61 | + .map(|s| if s.contains("++") { 1 } else { -1 }) |
| 62 | + .sum::<i32>() |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// submission codes end |
| 67 | + |
| 68 | +#[cfg(test)] |
| 69 | +mod tests { |
| 70 | + use super::*; |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn test_2011_example_1() { |
| 74 | + let operations = vec_string!["--X", "X++", "X++"]; |
| 75 | + |
| 76 | + let result = 1; |
| 77 | + |
| 78 | + assert_eq!(Solution::final_value_after_operations(operations), result); |
| 79 | + } |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn test_2011_example_2() { |
| 83 | + let operations = vec_string!["++X", "++X", "X++"]; |
| 84 | + |
| 85 | + let result = 3; |
| 86 | + |
| 87 | + assert_eq!(Solution::final_value_after_operations(operations), result); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_2011_example_3() { |
| 92 | + let operations = vec_string!["X++", "++X", "--X", "X--"]; |
| 93 | + |
| 94 | + let result = 0; |
| 95 | + |
| 96 | + assert_eq!(Solution::final_value_after_operations(operations), result); |
| 97 | + } |
| 98 | +} |
0 commit comments