2626. Array Reduce Transformation

更新於 發佈於 閱讀時間約 4 分鐘

(略),array 是用來將陣列的值進行累加,我們來看看怎麼怎麼達成吧:

Given an integer array nums, a reducer function fn, and an initial value init, return the final result obtained by executing the fn function on each element of the array, sequentially, passing in the return value from the calculation on the preceding element.

This result is achieved through the following operations: val = fn(init, nums[0]), val = fn(val, nums[1]), val = fn(val, nums[2]), ... until every element in the array has been processed. The ultimate value of val is then returned.

If the length of the array is 0, the function should return init.

Please solve it without using the built-in Array.reduce method.

 

Example 1:

Input: 
nums = [1,2,3,4]
fn = function sum(accum, curr) { return accum + curr; }
init = 0
Output: 10
Explanation:
initially, the value is init=0.
(0) + nums[0] = 1
(1) + nums[1] = 3
(3) + nums[2] = 6
(6) + nums[3] = 10
The final answer is 10.

Example 2:

Input: 
nums = [1,2,3,4]
fn = function sum(accum, curr) { return accum + curr * curr; }
init = 100
Output: 130
Explanation:
initially, the value is init=100.
(100) + nums[0] * nums[0] = 101
(101) + nums[1] * nums[1] = 105
(105) + nums[2] * nums[2] = 114
(114) + nums[3] * nums[3] = 130
The final answer is 130.

Example 3:

Input: 
nums = []
fn = function sum(accum, curr) { return 0; }
init = 25
Output: 25
Explanation: For empty arrays, the answer is always init.

 

解題思路

我們首先先建立一個函式,並包含題目要求的三個元素:數組 nums、一個 reducer 函數 fn 和一個初始值 init

const reduce = function(nums, fn, init) 

因為會需要迭代,所以我們要先宣告一個最終返回的值

let current = init

接著依照 nums 陣列的長度執行每一次 fn 函式的迴圈,並變更值

 for (let i = 0; i < nums.length; i++) {
current = fn(current, nums[i]);
}

最後再回傳 current 值就完成了


const reduce = function(nums, fn, init) {
let current = init
for (let i = 0; i < nums.length; i++) {
current = fn(current, nums[i]);;
}
return current
};




avatar-img
0會員
5內容數
留言
avatar-img
留言分享你的想法!
高的沙龍 的其他內容
問題 Write a function expect that helps developers test their code. It should take in any value val and return an object with the following two functio
問題 Write a function expect that helps developers test their code. It should take in any value val and return an object with the following two functio
你可能也想看
Google News 追蹤
Thumbnail
全新 vocus 挑戰活動「方格人氣王」來啦~四大挑戰任你選,留言 / 愛心 / 瀏覽數大 PK,還有新手專屬挑戰!無論你是 vocus 上活躍創作者或剛加入的新手,都有機會被更多人看見,獲得站上版位曝光&豐富獎勵!🏆
Thumbnail
本文探討AI筆記工具的優缺點、選擇建議及未來趨勢,比較NotebookLM、OneNote+Copilot、Notion AI、Obsidian+GPT插件和Palantir Foundry等工具,並強調安全注意事項及個人需求評估的重要性。
Thumbnail
這篇文章深入探討 PHP 中的 Array 與 Laravel 框架中的 Collection 之間的主要差異,並分析各自的優缺點。文章介紹了兩者在本質、方法連鎖、高階方法及 Eloquent 互動的區別,並提供實際的使用案例,幫助初學者更好地理解這兩種資料結構的特性。
Thumbnail
什麼是Array(陣列) array跟object一樣是一種資料的容器,不同的是array通常用來裝同類別的資料,就像是書架用來收納書。要在書架裡放書跟其他東西也是可以的,但就是有點奇怪,不是大家習慣的做法。 array透過[]宣告。 假設我們要儲存使用者喜歡的顏色這筆資料,每筆資料的類型
Thumbnail
這篇內容,將會講解什麼是陣列,以及與陣列相關的知識。包括陣列的簡介、陣列的資料限制、陣列的維度、一維陣列、二維陣列。
Thumbnail
題目敘述 Make Two Arrays Equal by Reversing Subarrays 題目給定兩個輸入陣列,請問能否透過子陣列的反轉讓兩個陣列相等? 子陣列的反轉操作次數不受限制。 如果可以,返回True 如果不行,返回False
前言 對標題上的這兩個項目有疑惑,不知道它們返回的資料的不同;查找資料後記錄下來,讓自己以後可以回來翻閱。 正文 numpy.ndarray.flatten:返回攤平的一維array,可參考NumPy: numpy.ndarray.flatten() function,有示意圖 te
Thumbnail
JavaScript30 傳送門:https://javascript30.com/ Console.table 第四個練習是透過一些 array method 來操作 array 資料。 整個練習最讓我 WOW 的,是 console.table,竟然有這麼酷的 console
Thumbnail
很明顯可以看到 parallelSort(T[], Comparator<T> 大概可以帶來 2.5 倍到接近 3 倍的效能增益 (和數量無關)。所以,結論是當需要處理大量資料的排序時,真的可以考慮使用 parallelSort(T[], Comparator<T>。
※ 常用arry型態的方法: 長度: length 查詢第N個元素: [] 查詢元素在第N個: indexOf( ) 判斷是否為array: isArray() 新增和刪除: push():新增後面的數值 unshift():新增前面的數值 pop():刪除後面的數值 sh
Thumbnail
分享在網路上看到的陣列題目。通常 for...of 的 value 是陣列中的每個值,那如果我們在迭代中對陣列操作會發生什麼事? 題目來源:https://x.com/_jayphelps/status/1774640511158022335?s=20
Thumbnail
全新 vocus 挑戰活動「方格人氣王」來啦~四大挑戰任你選,留言 / 愛心 / 瀏覽數大 PK,還有新手專屬挑戰!無論你是 vocus 上活躍創作者或剛加入的新手,都有機會被更多人看見,獲得站上版位曝光&豐富獎勵!🏆
Thumbnail
本文探討AI筆記工具的優缺點、選擇建議及未來趨勢,比較NotebookLM、OneNote+Copilot、Notion AI、Obsidian+GPT插件和Palantir Foundry等工具,並強調安全注意事項及個人需求評估的重要性。
Thumbnail
這篇文章深入探討 PHP 中的 Array 與 Laravel 框架中的 Collection 之間的主要差異,並分析各自的優缺點。文章介紹了兩者在本質、方法連鎖、高階方法及 Eloquent 互動的區別,並提供實際的使用案例,幫助初學者更好地理解這兩種資料結構的特性。
Thumbnail
什麼是Array(陣列) array跟object一樣是一種資料的容器,不同的是array通常用來裝同類別的資料,就像是書架用來收納書。要在書架裡放書跟其他東西也是可以的,但就是有點奇怪,不是大家習慣的做法。 array透過[]宣告。 假設我們要儲存使用者喜歡的顏色這筆資料,每筆資料的類型
Thumbnail
這篇內容,將會講解什麼是陣列,以及與陣列相關的知識。包括陣列的簡介、陣列的資料限制、陣列的維度、一維陣列、二維陣列。
Thumbnail
題目敘述 Make Two Arrays Equal by Reversing Subarrays 題目給定兩個輸入陣列,請問能否透過子陣列的反轉讓兩個陣列相等? 子陣列的反轉操作次數不受限制。 如果可以,返回True 如果不行,返回False
前言 對標題上的這兩個項目有疑惑,不知道它們返回的資料的不同;查找資料後記錄下來,讓自己以後可以回來翻閱。 正文 numpy.ndarray.flatten:返回攤平的一維array,可參考NumPy: numpy.ndarray.flatten() function,有示意圖 te
Thumbnail
JavaScript30 傳送門:https://javascript30.com/ Console.table 第四個練習是透過一些 array method 來操作 array 資料。 整個練習最讓我 WOW 的,是 console.table,竟然有這麼酷的 console
Thumbnail
很明顯可以看到 parallelSort(T[], Comparator<T> 大概可以帶來 2.5 倍到接近 3 倍的效能增益 (和數量無關)。所以,結論是當需要處理大量資料的排序時,真的可以考慮使用 parallelSort(T[], Comparator<T>。
※ 常用arry型態的方法: 長度: length 查詢第N個元素: [] 查詢元素在第N個: indexOf( ) 判斷是否為array: isArray() 新增和刪除: push():新增後面的數值 unshift():新增前面的數值 pop():刪除後面的數值 sh
Thumbnail
分享在網路上看到的陣列題目。通常 for...of 的 value 是陣列中的每個值,那如果我們在迭代中對陣列操作會發生什麼事? 題目來源:https://x.com/_jayphelps/status/1774640511158022335?s=20