列表

详情


2011. 执行操作后的变量值

存在一种仅支持 4 种操作和 1 个变量 X 的编程语言:

最初,X 的值是 0

给你一个字符串数组 operations ,这是由操作组成的一个列表,返回执行所有操作后, X最终值

 

示例 1:

输入:operations = ["--X","X++","X++"]
输出:1
解释:操作按下述步骤执行:
最初,X = 0
--X:X 减 1 ,X =  0 - 1 = -1
X++:X 加 1 ,X = -1 + 1 =  0
X++:X 加 1 ,X =  0 + 1 =  1

示例 2:

输入:operations = ["++X","++X","X++"]
输出:3
解释:操作按下述步骤执行: 
最初,X = 0
++X:X 加 1 ,X = 0 + 1 = 1
++X:X 加 1 ,X = 1 + 1 = 2
X++:X 加 1 ,X = 2 + 1 = 3

示例 3:

输入:operations = ["X++","++X","--X","X--"]
输出:0
解释:操作按下述步骤执行:
最初,X = 0
X++:X 加 1 ,X = 0 + 1 = 1
++X:X 加 1 ,X = 1 + 1 = 2
--X:X 减 1 ,X = 2 - 1 = 1
X--:X 减 1 ,X = 1 - 1 = 0

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: int finalValueAfterOperations(vector<string>& operations) { } };

rust 解法, 执行用时: 0 ms, 内存消耗: 2 MB, 提交时间: 2023-09-12 16:16:52

/* 解法一:常规
impl Solution {
    pub fn final_value_after_operations(operations: Vec<String>) -> i32 {
        operations.into_iter().map(|s|
            if s.contains('+') {
                1
            } else {
                -1
            }
        ).sum()
    }
}
*/

/* 解法二:match
impl Solution {
    pub fn final_value_after_operations(operations: Vec<String>) -> i32 {
        let mut res=0;
        for str in operations.iter(){
            match str.chars().nth(1){   
                Some('-')=>{res-=1;}
                Some('+')=>{res+=1;}
                _=>(),
            }
        }
        res 
    }
}
*/

// 解法三:函数式
impl Solution {
    pub fn final_value_after_operations(operations: Vec<String>) -> i32 {
        operations.iter().fold(0, |acc, x| match x.as_bytes()[1] {
            b'+' => acc + 1,
            b'-' => acc - 1,
            _ => acc,
        })
    }
}

php 解法, 执行用时: 16 ms, 内存消耗: 19.1 MB, 提交时间: 2023-09-12 16:13:48

class Solution {

    /**
     * @param String[] $operations
     * @return Integer
     */
    function finalValueAfterOperations($operations) {
        $ans = 0;
        foreach ( $operations as $s ) {
            $ans += $s[1] == '+' ? 1 : -1;
        }
        return $ans;
    }
}

cpp 解法, 执行用时: 4 ms, 内存消耗: 13.6 MB, 提交时间: 2022-12-23 09:26:20

class Solution {
public:
    int finalValueAfterOperations(vector<string>& operations) {
        int ans = 0;
        for (auto& s : operations) ans += (s[1] == '+' ? 1 : -1);
        return ans;
    }
};

java 解法, 执行用时: 0 ms, 内存消耗: 41.3 MB, 提交时间: 2022-12-23 09:26:05

class Solution {
    public int finalValueAfterOperations(String[] operations) {
        int ans = 0;
        for (var s : operations) {
            ans += (s.charAt(1) == '+' ? 1 : -1);
        }
        return ans;
    }
}

javascript 解法, 执行用时: 64 ms, 内存消耗: 42.6 MB, 提交时间: 2022-12-23 09:25:40

/**
 * @param {string[]} operations
 * @return {number}
 */
var finalValueAfterOperations = function(operations) {
    let x = 0;
    for (const op of operations) {
        if ("X++" === op || "++X" === op) {
            x++;
        } else {
            x--;
        }
    }
    return x;
};

golang 解法, 执行用时: 4 ms, 内存消耗: 3.3 MB, 提交时间: 2022-12-23 09:25:15

func finalValueAfterOperations(operations []string) (ans int) {
	for _, s := range operations {
		if s[1] == '+' {
			ans += 1
		} else {
			ans -= 1
		}
	}
	return
}

python3 解法, 执行用时: 32 ms, 内存消耗: 15.1 MB, 提交时间: 2022-12-23 09:24:55

class Solution:
    def finalValueAfterOperations(self, operations: List[str]) -> int:
        return sum(1 if s[1] == '+' else -1 for s in operations)

golang 解法, 执行用时: 0 ms, 内存消耗: 3.5 MB, 提交时间: 2021-09-22 14:37:22

func finalValueAfterOperations(operations []string) int {
    ans := 0
    for _, op := range operations {
        switch op {
            case "--X", "X--":
                ans--
            case "++X", "X++":
                ans++
            default:
                break
        }
    }
    return ans
}

上一题