{ "data": { "question": { "questionId": "2789", "questionFrontendId": "2665", "categoryTitle": "JavaScript", "boundTopicId": 2258605, "title": "Counter II", "titleSlug": "counter-ii", "content": "
Write a function createCounter
. It should accept an initial integer init
. It should return an object with three functions.
The three functions are:
\n\nincrement()
increases the current value by 1 and then returns it.decrement()
reduces the current value by 1 and then returns it.reset()
sets the current value to init
and then returns it.\n
Example 1:
\n\n\nInput: init = 5, calls = ["increment","reset","decrement"]\nOutput: [6,5,4]\nExplanation:\nconst counter = createCounter(5);\ncounter.increment(); // 6\ncounter.reset(); // 5\ncounter.decrement(); // 4\n\n\n
Example 2:
\n\n\nInput: init = 0, calls = ["increment","increment","decrement","reset","reset"]\nOutput: [1,2,1,0,0]\nExplanation:\nconst counter = createCounter(0);\ncounter.increment(); // 1\ncounter.increment(); // 2\ncounter.decrement(); // 1\ncounter.reset(); // 0\ncounter.reset(); // 0\n\n\n
\n
Constraints:
\n\n-1000 <= init <= 1000
0 <= calls.length <= 1000
calls[i]
is one of "increment", "decrement" and "reset"请你写一个函数 createCounter
。这个函数接收一个初始的整数值 init
。并返回一个包含三个函数的对象。
这三个函数是:
\n\nincrement()
将当前值加 1 并返回。decrement()
将当前值减 1 并返回。reset()
将当前值设置为 init
并返回。\n\n
示例 1:
\n\n\n输入:init = 5, calls = [\"increment\",\"reset\",\"decrement\"]\n输出:[6,5,4]\n解释:\nconst counter = createCounter(5);\ncounter.increment(); // 6\ncounter.reset(); // 5\ncounter.decrement(); // 4\n\n\n
示例 2:
\n\n\n输入:init = 0, calls = [\"increment\",\"increment\",\"decrement\",\"reset\",\"reset\"]\n输出:[1,2,1,0,0]\n解释:\nconst counter = createCounter(0);\ncounter.increment(); // 1\ncounter.increment(); // 2\ncounter.decrement(); // 1\ncounter.reset(); // 0\ncounter.reset(); // 0\n\n\n
\n\n
提示:
\n\n-1000 <= init <= 1000
0 <= calls.length <= 1000
calls[i]
是 “increment”、“decrement” 和 “reset” 中的一个\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\" TypeScript 5.1.6<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "5\n[\"increment\",\"reset\",\"decrement\"]\n0\n[\"increment\",\"increment\",\"decrement\",\"reset\",\"reset\"]",
"__typename": "QuestionNode"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n