<div style="display: none;" hidden="true" aria-hidden="true" data-nosnippet>Are you an LLM? You can read better optimized documentation at /pages/990dd4.md for this page in Markdown format</div>
执行操作后的变量值
| Category | Difficulty | Likes | Dislikes |
|---|---|---|---|
| algorithms | Easy (86.30%) | 23 | - |
<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Tags</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><a href="https://leetcode.com/tag/Unknown" title="https://leetcode.com/tag/Unknown" style="color: var(--vscode-textLink-foreground); text-decoration: none;"><code style="color: var(--vscode-textLink-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></a></p></details>
<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Companies</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><code style="color: var(--vscode-textPreformat-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></p></details>
存在一种仅支持 4 种操作和 1 个变量 X 的编程语言:
++X和X++使变量X的值 加1--X和X--使变量X的值 减1
最初,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提示:
1 <= operations.length <= 100operations[i]将会是"++X"、"X++"、"--X"或"X--"
Code Now
/*
* @Author: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Date: 2022-11-07 10:50:28
* @LastEditTime: 2022-11-07 11:11:54
* @LastEditors: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Description:
* @FilePath: /leetcode/2011.执行操作后的变量值.ts
*/
/*
* @lc app=leetcode.cn id=2011 lang=typescript
*
* [2011] 执行操作后的变量值
*
* https://leetcode.cn/problems/final-value-of-variable-after-performing-operations/description/
*
* algorithms
* Easy (86.30%)
* Likes: 23
* Dislikes: 0
* Total Accepted: 19.6K
* Total Submissions: 22.7K
* Testcase Example: '["--X","X++","X++"]'
*
* 存在一种仅支持 4 种操作和 1 个变量 X 的编程语言:
*
*
* ++X 和 X++ 使变量 X 的值 加 1
* --X 和 X-- 使变量 X 的值 减 1
*
*
* 最初,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
*
*
*
*
* 提示:
*
*
* 1 <= operations.length <= 100
* operations[i] 将会是 "++X"、"X++"、"--X" 或 "X--"
*
*
*/
// @lc code=start
function finalValueAfterOperations(operations: string[]): number {
let X = 0;
const map = [
{ label: "++X", value: 1 },
{ label: "X++", value: 1 },
{ label: "--X", value: -1 },
{ label: "X--", value: -1 },
];
operations.forEach((e) => {
const val = map.find((v) => v.label === e)?.value;
X += val ?? 0;
});
return X;
}
function finalValueAfterOperations1(operations: string[]): number {
let X = 0;
const map = new Map();
map.set("++X", 1);
map.set("X++", 1);
map.set("--X", -1);
map.set("X--", -1);
for (let i = 0; i < operations.length; i++) {
X += map.get(operations[i])
}
return X;
}
// @lc code=end