Skip to content

Commit 7345930

Browse files
committedDec 23, 2021
solves final value of variable after performing operations
1 parent a51d6cd commit 7345930

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@
480480
| 1995 | [Count Special Quadruplets](https://leetcode.com/problems/count-special-quadruplets) | [![Java](assets/java.png)](src/CountSpecialQuadruplets.java) | |
481481
| 2000 | [Reverse Prefix of Word](https://leetcode.com/problems/reverse-prefix-of-word) | [![Java](assets/java.png)](src/ReversePrefixOfWord.java) | |
482482
| 2006 | [Count Number of Pairs With Absolute Difference K](https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k) | [![Java](assets/java.png)](src/CountNumberOfPairsWithAbsoluteDifferenceK.java) | |
483-
| 2011 | [Final Value of Variable After Performing Operations](https://leetcode.com/problems/final-value-of-variable-after-performing-operations) | | |
483+
| 2011 | [Final Value of Variable After Performing Operations](https://leetcode.com/problems/final-value-of-variable-after-performing-operations) | [![Java](assets/java.png)](src/FinalValueOfVariableAfterPerformingOperations.java) | |
484484
| 2016 | [Maximum Difference Between Increasing Elements](https://leetcode.com/problems/maximum-difference-between-increasing-elements) | | |
485485
| 2022 | [Convert 1D Array Into 2D Array](https://leetcode.com/problems/convert-1d-array-into-2d-array) | | |
486486
| 2027 | [Minimum Moves to Convert String](https://leetcode.com/problems/minimum-moves-to-convert-string) | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// https://leetcode.com/problems/final-value-of-variable-after-performing-operations
2+
// T: O(|operations|)
3+
// S: O(1)
4+
5+
public class FinalValueOfVariableAfterPerformingOperations {
6+
public int finalValueAfterOperations(String[] operations) {
7+
int x = 0;
8+
for (String operation : operations) {
9+
x = apply(x, operation);
10+
}
11+
return x;
12+
}
13+
14+
private int apply(int x, String operation) {
15+
return switch (operation) {
16+
case "++X", "X++" -> x + 1;
17+
case "--X", "X--" -> x - 1;
18+
default -> x;
19+
};
20+
}
21+
}

0 commit comments

Comments
 (0)