Some people's understanding of recursion remains at the same level as a method that is inefficient at finding a factorial than a loop. Actually, recursion and loop processing are different. Taking the traversal array as an example: loops are suitable for traversing on the same dimension (with unlimited length of a single layer), while recursion is suitable for Traversing across dimensions (with unlimited layers .... SyntaxHighlighter. all (); some people's understanding of recursion is still stuck in "It is an inefficient method to calculate factorial than loop ". Actually, recursion and loop processing are different. Taking the "traversal array" issue for example: loops are suitable for traversing on the same dimension (with unlimited single-layer length), while recursion is suitable for Traversing across dimensions (with unlimited layers. For example, traverse the following one-dimensional array: [javascript] var a1 = [1]; var a2 = [1, 2]; var a3 = [1, 2, 3]; although they have different lengths, it is very easy and elegant to handle them cyclically: [javascript] var dumpArrayByLoop = function (a) {for (var I = 0; I <. length; I ++) {println (a [I]) ;}; if recursion is used, it looks awkward: [javascript] var dumpArrayByRecur = function (I,) {if (I <. length) {println (a [I]); dumpArrayByRecur (I + 1, a) ;}}; they can output the same results, but the recursive version looks clumsy in comparison. Now, if the metadata changes, the dimension is extended to two-dimensional. [Javascript] var a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]; in this case, you need to set another loop to a dual loop: [javascript] var dumpArrayByLoop = function (a) {for (var I = 0; I <. length; I ++) {for (var j = 0; j <a [I]. length; j ++) {println (a [I] [j]) ;}}; if the data dimension continues to expand, it becomes three-dimensional, four-dimensional ...... Even a dynamic n-dimensional array. What should I do if I use a loop? In such a situation that the number of layers is very deep or even uncertain, we need to use recursion to solve the problem of cross-layer. [Javascript] www.2cto. comvar isArray = function (a) {return Object. prototype. toString. call (a) = '[object Array]';}; var dumpArrayByRecur = function (a) {if (isArray (a) {for (var I = 0; I <. length; I ++) {dumpArray (a [I]) ;}} else {println (a) ;}; in the above Code, if the subnode is an array, it uses recursion to enter the next layer, while traversal on the same layer uses loops.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.