mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
42 lines
1.5 KiB
HTML
42 lines
1.5 KiB
HTML
<p>Given an integer array <code>arr</code> and a mapping function <code>fn</code>, return a new array with a transformation applied to each element.</p>
|
|
|
|
<p>The returned array should be created such that <code>returnedArray[i] = fn(arr[i], i)</code>.</p>
|
|
|
|
<p>Please solve it without the built-in <code>Array.map</code> method.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> arr = [1,2,3], fn = function plusone(n) { return n + 1; }
|
|
<strong>Output:</strong> [2,3,4]
|
|
<strong>Explanation:</strong>
|
|
const newArray = map(arr, plusone); // [2,3,4]
|
|
The function increases each value in the array by one.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> arr = [1,2,3], fn = function plusI(n, i) { return n + i; }
|
|
<strong>Output:</strong> [1,3,5]
|
|
<strong>Explanation:</strong> The function increases each value by the index it resides in.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> arr = [10,20,30], fn = function constant() { return 42; }
|
|
<strong>Output:</strong> [42,42,42]
|
|
<strong>Explanation:</strong> The function always returns 42.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>0 <= arr.length <= 1000</code></li>
|
|
<li><code><font face="monospace">-10<sup>9</sup> <= arr[i] <= 10<sup>9</sup></font></code></li>
|
|
<li><code>fn</code> returns a number</li>
|
|
</ul>
|