{ "data": { "question": { "questionId": "2732", "questionFrontendId": "2620", "categoryTitle": "JavaScript", "boundTopicId": 2222282, "title": "Counter", "titleSlug": "counter", "content": "
Given an integer n
, return a counter
function. This counter
function initially returns n
and then returns 1 more than the previous value every subsequent time it is called (n
, n + 1
, n + 2
, etc).
\n
Example 1:
\n\n\nInput: \nn = 10 \n["call","call","call"]\nOutput: [10,11,12]\nExplanation: \ncounter() = 10 // The first time counter() is called, it returns n.\ncounter() = 11 // Returns 1 more than the previous time.\ncounter() = 12 // Returns 1 more than the previous time.\n\n\n
Example 2:
\n\n\nInput: \nn = -2\n["call","call","call","call","call"]\nOutput: [-2,-1,0,1,2]\nExplanation: counter() initially returns -2. Then increases after each sebsequent call.\n\n\n
\n
Constraints:
\n\n-1000 <= n <= 1000
0 <= calls.length <= 1000
calls[i] === "call"
给定一个整型参数 n
,请你编写并返回一个 counter
函数。这个 counter
函数最初返回 n
,每次调用它时会返回前一个值加 1 的值 ( n
, n + 1
, n + 2
,等等)。
\n\n
示例 1:
\n\n\n输入:\nn = 10 \n[\"call\",\"call\",\"call\"]\n输出:[10,11,12]\n解释:\ncounter() = 10 // 第一次调用 counter(),返回 n。\ncounter() = 11 // 返回上次调用的值加 1。\ncounter() = 12 // 返回上次调用的值加 1。\n\n\n
示例 2:
\n\n\n输入:\nn = -2\n[\"call\",\"call\",\"call\",\"call\",\"call\"]\n输出:[-2,-1,0,1,2]\n解释:counter() 最初返回 -2。然后在每个后续调用后增加 1。\n\n\n
\n\n
提示:
\n\n-1000 <= n <= 1000
0 <= calls.length <= 1000
calls[i] === \"call\"
\\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": "10\n[\"call\",\"call\",\"call\"]\n-2\n[\"call\",\"call\",\"call\",\"call\",\"call\"]",
"__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