mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-12 02:41:42 +08:00
批量更新数据
This commit is contained in:
@@ -1,64 +1,64 @@
|
||||
<p>请你编写一个函数,它的参数为一个整数数组 <code>nums</code> 、一个计算函数 <code>fn</code> 和初始值<code>init</code> 。返回一个数组 <strong>归约后 </strong>的值。</p>
|
||||
|
||||
<p>你可以定义一个数组 <strong>归约后 </strong>的值,然后应用以下操作: <code>val = fn(init, nums[0])</code> , <code>val = fn(val, nums[1])</code> , <code>val = fn(val, nums[2])</code> ,<code>...</code> 直到数组中的每个元素都被处理完毕。返回 <code>val</code> 的最终值。</p>
|
||||
|
||||
<p>如果数组的长度为 0,它应该返回 <code>init</code> 的值。</p>
|
||||
|
||||
<p>请你在不使用内置数组方法的 <code>Array.reduce</code> 前提下解决这个问题。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
nums = [1,2,3,4]
|
||||
fn = function sum(accum, curr) { return accum + curr; }
|
||||
init = 0
|
||||
<strong>输出:</strong>10
|
||||
<strong>解释:</strong>
|
||||
初始值为 init=0 。
|
||||
(0) + nums[0] = 1
|
||||
(1) + nums[1] = 3
|
||||
(3) + nums[2] = 6
|
||||
(6) + nums[3] = 10
|
||||
Val 最终值为 10。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
nums = [1,2,3,4]
|
||||
fn = function sum(accum, curr) { return accum + curr * curr; }
|
||||
init = 100
|
||||
<strong>输出:</strong>130
|
||||
<strong>解释:</strong>
|
||||
初始值为 init=100 。
|
||||
(100) + nums[0]^2 = 101
|
||||
(101) + nums[1]^2 = 105
|
||||
(105) + nums[2]^2 = 114
|
||||
(114) + nums[3]^2 = 130
|
||||
Val 最终值为 130。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
nums = []
|
||||
fn = function sum(accum, curr) { return 0; }
|
||||
init = 25
|
||||
<strong>输出:</strong>25
|
||||
<b>解释:</b>这是一个空数组,所以返回 init 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
<li><code>0 <= init <= 1000</code></li>
|
||||
</ul>
|
||||
<p>给定一个整数数组 <code>nums</code>、一个 reducer 函数 <code>fn</code> 和一个初始值 <code>init</code>,返回通过依次对数组的每个元素执行 <code>fn</code> 函数得到的最终结果。</p>
|
||||
|
||||
<p>通过以下操作实现这个结果:<code>val = fn(init, nums[0]),val = fn(val, nums[1]),val = fn(val, nums[2]),...</code> 直到处理数组中的每个元素。然后返回 <code>val</code> 的最终值。</p>
|
||||
|
||||
<p>如果数组的长度为 0,则函数应返回 <code>init</code>。</p>
|
||||
|
||||
<p>请你在不使用内置数组方法的 <code>Array.reduce</code> 前提下解决这个问题。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
nums = [1,2,3,4]
|
||||
fn = function sum(accum, curr) { return accum + curr; }
|
||||
init = 0
|
||||
<strong>输出:</strong>10
|
||||
<strong>解释:</strong>
|
||||
初始值为 init=0 。
|
||||
(0) + nums[0] = 1
|
||||
(1) + nums[1] = 3
|
||||
(3) + nums[2] = 6
|
||||
(6) + nums[3] = 10
|
||||
Val 最终值为 10。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
nums = [1,2,3,4]
|
||||
fn = function sum(accum, curr) { return accum + curr * curr; }
|
||||
init = 100
|
||||
<strong>输出:</strong>130
|
||||
<strong>解释:</strong>
|
||||
初始值为 init=100 。
|
||||
(100) + nums[0]^2 = 101
|
||||
(101) + nums[1]^2 = 105
|
||||
(105) + nums[2]^2 = 114
|
||||
(114) + nums[3]^2 = 130
|
||||
Val 最终值为 130。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
nums = []
|
||||
fn = function sum(accum, curr) { return 0; }
|
||||
init = 25
|
||||
<strong>输出:</strong>25
|
||||
<b>解释:</b>这是一个空数组,所以返回 init 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
<li><code>0 <= init <= 1000</code></li>
|
||||
</ul>
|
||||
|
Reference in New Issue
Block a user