什么是递归?递归一词来自recurring,意思是一次又一次地回到过去。递归函数是通过一步步改变输入来一次又一次调用自身的函数。这里,将输入改变一级意味着将输入减少或增加一级。
每当递归函数达到基本条件时,它就会停止自身的执行。让我们通过一个例子来理解什么是基本条件。例如,我们需要求一个数的阶乘。我们通过将输入减 1 来调用阶乘函数,并且每当输入达到 1 时就需要停止。因此,这里 1 作为基本条件。
语法用户可以使用下面的语法来理解 javascript 中的递归。
function recur(val) { if (base condition) { return; } // perform some action // decrease the value of val by one step return recur(newval);}
在上面的语法中,用户可以观察到当基本条件变为 true 时我们返回 null 以停止函数的执行。如果基本条件为 false,我们将使用输入值执行某些操作,并使用新的参数值再次调用 recur() 函数。
现在,让我们看一下递归的各种示例。在这里,我们将学习首先使用 for 循环实现迭代算法,然后将其转换为递归方法。
示例 1(使用 for 循环求 1 到 n 个数字的和)在下面的示例中,我们编写了 sumofn() 函数来获取 1 到 n 个数字的总和。我们使用 for 循环进行了 n 次迭代,并且在每次迭代中,我们将 i 的值添加到 sum 变量中。
最后返回sum变量的值。
<html><body> <h3>using the <i> iterative approach </i> to find sum of n numbers in javascript</h3> <div id = content> </div> <script> let content = document.getelementbyid('content'); // function to find the sum of n numbers using an iterative approach function sumofn(n) { let sum = 0; for (let i = n; i >= 1; i--) { sum += i; } return sum; } content.innerhtml += the sum of 1 to 10 numbers is + sumofn(10) + <br>; content.innerhtml += the sum of 1 to 20 numbers is + sumofn(20) + <br>; </script></body></html>
在上面的示例中,我们使用迭代方法来求 n 个数字的总和。现在,我们将使用递归方法来做同样的事情。
示例 2(使用递归函数求 1 到 n 个数字的和)sumofn() 函数是下面示例中的递归函数。我们通过将参数的值减 1 来重复调用 sumofn() 函数。 sumofn(n1) 返回 n-1 个数字的总和,我们将 n 添加到它以获得 n 个数字的总和。每当 n 的值变为 1 时,它就会返回 1,这作为停止函数执行的基本条件。
<html><body> <h3>using the <i> recursive approach </i> to find sum of n numbers in javascript</h3> <div id = content> </div> <script> let content = document.getelementbyid('content'); // function to find the sum of n numbers using a recursive approach function sumofn(n) { // base condition if (n == 1) { return 1; } // call function recursively by decreasing the value of n by 1. return n + sumofn(n - 1); } content.innerhtml += the sum of 1 to 10 numbers is + sumofn(10) + <br>; content.innerhtml += the sum of 1 to 20 numbers is + sumofn(20) + <br>; </script></body></html>
让我们了解一下上面的递归函数是如何工作的。下面,用户可以逐步了解递归函数调用是如何发生的。
sumofn(5);return 5 + sumofn(4); return 4 + sumofn(3); return 3 + sumofn(2); return 2 + sumofn(1); return 1; return 2 + 1; return 3 + 3; return 4 + 6;
示例 3(合并数组所有字符串的迭代方法)在下面的示例中,我们创建了字符串数组。我们创建了 mergestring() 函数来将数组的所有字符串合并为一个字符串。我们使用 for 循环遍历数组,并将所有字符串一一合并到“str”变量中。
<html><body> <h3>using the <i> iterative approach </i> to merge all strings of the array in javascript</h3> <div id = content> </div> <script> let content = document.getelementbyid('content'); // function to merge all strings of the array using for loop function mergestring(arr) { let str = ''; for (let i = 0; i < arr.length; i++) { str += arr[i]; } return str; } let arr = ['i', ' ', 'am', ' ', 'a', ' ', 'programmer']; content.innerhtml += the original array is: + arr + <br>; content.innerhtml += after merging all strings of the array into the single string is + mergestring(arr) + <br>; </script></body></html>
示例 4(合并数组所有字符串的递归方法)在下面的示例中,我们已将 mergestring() 函数转换为递归函数。我们获取数组的第一个元素,并将其与 mergestring() 函数的返回结果合并。 mergestring() 函数返回合并后的最后 n-1 个数组元素。此外,我们使用 slice() 方法从数组中删除第一个元素。
当数组中只剩下一个元素时,它返回相同的元素,该元素作为基本条件。
<html><body> <h3>using the <i> recursive approach </i> to merge all strings of the array in javascript</h3> <div id = content> </div> <script> let content = document.getelementbyid('content'); // function to merge all strings of the array using recursion function mergestring(arr) { // based condition if (arr.length == 1) { return arr[0]; } // remove the first element from the array using the slice() method. return arr[0] + + mergestring(arr.slice(1)); } let arr = [i, am, a, web, developer]; content.innerhtml += the original array is: + arr + <br>; content.innerhtml += after merging all strings of the array into the single string is + mergestring(arr) + <br>; </script></body></html>
用户应该使用哪种方法,迭代还是递归?主要问题是哪种方法更好,迭代还是递归,以及用户应该使用哪种方法。
在某些情况下,迭代方法比递归方法更快。此外,递归在迭代过程中需要更多的内存。对于某些算法(例如分治法),递归更有用,因为我们需要使用递归方法编写更少的代码。此外,如果递归方法中未触发基本条件,用户可能会面临内存泄漏问题。
如果我们可以将代码分解成更小的部分,我们应该使用递归方法,而为了提高代码的性能,我们应该使用迭代方法。
以上就是如何理解 javascript 中的递归?的详细内容。