- Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Thank you very much for reading this article. Welcome [👍 like] [⭐ collect] [📝 comment] ~ Give up is not difficult, But insists it must be very cool ~ hope we all can be a little bit of progress every day ~ in this paper, from two headed white hat original ~ https://juejin.cn/user/2771185768884824/posts blog
2011. Variable values after operation:
There is a programming language that supports only four operations and one variable X:
Plus plus X and X++ add 1 to the value of the variable X– X and X– subtract 1 from the value of the variable X originally, X was 0
You’re given an array of strings called operations, which is a list of operations that return the final value of X after all the operations have been performed.
Sample 1
Input: operations = ["--X","X++","X++"] Output: 1 Explanation: The operation is performed as follows: Initially, X = 0 --X: X minus 1, X = 0-1 = -1 X++ : X++ : X+ 1, X = 0 + 1 = 1Copy the code
The sample 2
Input: operations = ["++X","++X","X++"] Output: 3 Explanation: The operations are performed as follows: Initially, X = 0 ++X: X+ 1, X = 0 + 1 = 1 ++X: X++ : X+ 1, X = 2 + 1 = 3Copy the code
Sample 3
Input: operations = ["X++","++X","--X","X--"] Output: 0 Explanation: The operations are performed as follows: Initially, X = 0 X++ : X plus 1, X = 0 + 1 = 1 ++X: X + 1, X = 1 + 1 = 2 --X: X minus 1, X = 2-1 = 1 --X: X minus 1, X = 1-1 = 0Copy the code
prompt
- 1 <= operations.length <= 100
- Operations [I] will be “++X”, “X++”, “–X”, or “X–“
Analysis of the
- There are four operation symbols, but only two operations. We want “++X”, “X++” as addition, “–X” or “X–” as subtraction. The intuitive way is to go to map, switch, if else, whatever.
- Each operation symbol is three characters, with an X at the beginning or the end, but only two characters in the middle, namely ‘+’ and ‘-‘.
Answer key
java
class Solution {
public int finalValueAfterOperations(String[] operations) {
int ans = 0;
for (String operation : operations) {
// Operation type
char op = operation.charAt(1);
switch (op) {
case '+':
ans++;
break;
case The '-':
ans--;
break; }}returnans; }}Copy the code
c
int finalValueAfterOperations(char ** operations, int operationsSize){
int ans = 0;
for (int i = 0; i < operationsSize; ++i) {
char op = operations[i][1];
switch (op) {
case '+':
++ans;
break;
case The '-':
--ans;
break; }}return ans;
}
Copy the code
c++
class Solution {
public:
int finalValueAfterOperations(vector<string>& operations) {
int ans = 0;
for (auto &operation : operations) {
char op = operation[1];
switch (op) {
case '+':
++ans;
break;
case The '-':
--ans;
break; }}returnans; }};Copy the code
python
class Solution:
def finalValueAfterOperations(self, operations: List[str]) - >int:
ans = 0
for operation in operations:
if operation[1] = ='+':
ans += 1
else:
ans -= 1
return ans
Copy the code
go
func finalValueAfterOperations(operations []string) int {
ans := 0
for _, operation := range operations {
op := operation[1]
switch op {
case '+':
ans++
case The '-':
ans--
}
}
return ans
}
Copy the code
rust
impl Solution {
pub fn final_value_after_operations(operations: Vec<String- > >)i32 {
operations.iter().map(|operation| {
if operation.contains('+') {
1
} else{-1
}
}).sum()
}
}
Copy the code