LeetCode Logo Problem List 0 avatar Premium Debugging... Debugging... Run Description…
- User
LeetCode Logo Problem List 0
avatar Premium Debugging... Debugging...
Run Description Editorial Editorial Solutions Solutions Submissions Submissions
Code Testcase Test Result Test Result 2695. Array Wrapper Easy Companies Create a class ArrayWrapper that accepts an array of integers in its constructor. This class should have two features:
When two instances of this class are added together with the + operator, the resulting value is the sum of all the elements in both arrays. When the String() function is called on the instance, it will return a comma separated string surrounded by brackets. For example, [1,2,3].
Example 1:
Input: nums = [[1,2],[3,4]], operation = "Add" Output: 10 Explanation: const obj1 = new ArrayWrapper([1,2]); const obj2 = new ArrayWrapper([3,4]); obj1 + obj2; // 10 Example 2:
Input: nums = [[23,98,42,70]], operation = "String" Output: "[23,98,42,70]" Explanation: const obj = new ArrayWrapper([23,98,42,70]); String(obj); // "[23,98,42,70]" Example 3:
Input: nums = [[],[]], operation = "Add" Output: 0 Explanation: const obj1 = new ArrayWrapper([]); const obj2 = new ArrayWrapper([]); obj1 + obj2; // 0
Constraints:
0 <= nums.length <= 1000 0 <= nums[i] <= 1000 Note: nums is the array passed to the constructor Seen this question in a real interview before? 1/5 Yes No Accepted 43.4K Submissions 49.2K Acceptance Rate 88.3% Companies Discussion (20)
Choose a type
Copyright ©️ 2024 LeetCode All rights reserved
232
20
JavaScript Auto
1234567891011121314151617 /**
- @param {number[]} nums
- @return {void} */ var ArrayWrapper = function(nums) {
};
/**
- @return {number}
Saved Case 1 Case 2 Case 3
nums = [[1,2],[3,4]] operation = "Add" 1 [[1,2],[3,4]] "Add" [[23,98,42,70]] "String" [[],[]] "Add"
Tag
/**
- @param {number[]} nums
- @return {void} */ var ArrayWrapper = function(nums) {
};
/**
- @return {number} */ ArrayWrapper.prototype.valueOf = function() {
}
/**
- @return {string} */ ArrayWrapper.prototype.toString = function() {
}
/**
- const obj1 = new ArrayWrapper([1,2]);
- const obj2 = new ArrayWrapper([3,4]);
- obj1 + obj2; // 10
- String(obj1); // "[1,2]"
- String(obj2); // "[3,4]" */
- Assistant
To solve this problem, we need to implement the
ArrayWrapperclass with two specific behaviors: