{ "data": { "question": { "questionId": "2805", "questionFrontendId": "2695", "categoryTitle": "JavaScript", "boundTopicId": 2275511, "title": "Array Wrapper", "titleSlug": "array-wrapper", "content": "
Create a class ArrayWrapper
that accepts an array of integers in its constructor. This class should have two features:
+
operator, the resulting value is the sum of all the elements in both arrays.String()
function is called on the instance, it will return a comma separated string surrounded by brackets. For example, [1,2,3]
.\n
Example 1:
\n\n\nInput: nums = [[1,2],[3,4]], operation = "Add"\nOutput: 10\nExplanation:\nconst obj1 = new ArrayWrapper([1,2]);\nconst obj2 = new ArrayWrapper([3,4]);\nobj1 + obj2; // 10\n\n\n
Example 2:
\n\n\nInput: nums = [[23,98,42,70]], operation = "String"\nOutput: "[23,98,42,70]"\nExplanation:\nconst obj = new ArrayWrapper([23,98,42,70]);\nString(obj); // "[23,98,42,70]"\n\n\n
Example 3:
\n\n\nInput: nums = [[],[]], operation = "Add"\nOutput: 0\nExplanation:\nconst obj1 = new ArrayWrapper([]);\nconst obj2 = new ArrayWrapper([]);\nobj1 + obj2; // 0\n\n\n
\n
Constraints:
\n\n0 <= nums.length <= 1000
0 <= nums[i] <= 1000
Note: nums is the array passed to the constructor
创建一个名为 ArrayWrapper
的类,它在其构造函数中接受一个整数数组作为参数。该类应具有以下两个特性:
+
运算符将两个该类的实例相加时,结果值为两个数组中所有元素的总和。String()
函数时,它将返回一个由逗号分隔的括在方括号中的字符串。例如,[1,2,3]
。\n\n
示例 1:
\n\n\n输入:nums = [[1,2],[3,4]], operation = \"Add\"\n输出:10\n解释:\nconst obj1 = new ArrayWrapper([1,2]);\nconst obj2 = new ArrayWrapper([3,4]);\nobj1 + obj2; // 10\n\n\n
示例 2:
\n\n\n输入:nums = [[23,98,42,70]], operation = \"String\"\n输出:\"[23,98,42,70]\"\n解释:\nconst obj = new ArrayWrapper([23,98,42,70]);\nString(obj); // \"[23,98,42,70]\"\n\n\n
示例 3:
\n\n\n输入:nums = [[],[]], operation = \"Add\"\n输出:0\n解释:\nconst obj1 = new ArrayWrapper([]);\nconst obj2 = new ArrayWrapper([]);\nobj1 + obj2; // 0\n\n\n
\n\n
提示:
\n\n0 <= nums.length <= 1000
0 <= nums[i] <= 1000
注意:nums 是传递给构造函数的数组。
\\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": "[[1,2],[3,4]]\n\"Add\"\n[[23,98,42,70]]\n\"String\"\n[[],[]]\n\"Add\"",
"__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