mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			65 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>请你编写一个函数,它的参数为一个整数数组 <code>nums</code> 、一个计算函数 <code>fn</code> 和初始值 <font color="#c7254e"><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size:12.6px"><span style="background-color:#f9f2f4">init </span></span></font></font>。返回一个数组 <strong>归约后 </strong>的值。</p>
 | 
						||
 | 
						||
<p>你可以定义一个数组 <strong>归约后 </strong>的值,然后应用以下操作: <code>val = fn(init, nums[0])</code> , <code>val = fn(val, nums[1])</code> , <code>val = fn(val, arr[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=0 。
 | 
						||
(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>
 |