晴明的博客园 GitHub      CodePen      CodeWars     

[js] 递归(old)

#

            function reverse(str) {
                console.log(str);
                return str.length > 1 ? reverse(str.slice(1)) + str[0] : str;
            }

            function reverse(str) {
                console.log(str);
                return str.length == 1 ? str : reverse(str.substr(1)) + str[0];
            }

            reverse("hello world");
            reverse("abcd");
            reverse("12345");

 #

            //一般姿势
            function factorial(num) {
                if (num <= 1) {
                    return 1;
                } else {
                    return num * factorial(num - 1);
                }
            }
            //arguments.callee
            function factorial(num) {
                if (num <= 1) {
                    return 1;
                } else {
                    return num * arguments.callee(num - 1);
                }
            }
            //严格模式下的写法
            var factorial = (function f(num) {
                if (num <= 1) {
                    return 1;
                } else {
                    return num * f(num - 1);
                }
            })
            console.log(factorial(5));

 #

                //汉诺塔
            function hanoi(disc, src, aux, dst) {
                if (disc > 0) {
                    hanoi(disc - 1, src, dst, aux);
                    console.log('Move disc ' + disc +
                        ' from ' + src + ' to ' + dst);
                    hanoi(disc - 1, aux, src, dst);
                }
            };
            hanoi(3, '柱子1', '柱子2', '柱子3');
            //遍历节点并计数
            var walk_the_DOM = function walk(node, func) {
                func(node);
                node = node.firstChild;
                while (node) {
                    walk(node, func);
                    node = node.nextSibling;
                }
            };
            var getElementsByAttribute = function(att, value) {
                var results = [];
                walk_the_DOM(document.body, function(node) {
                    var actual = node.nodeType === 1 && node.getAttribute(att);
                    if (typeof actual === 'string' &&
                        (actual === value || typeof value !== 'string')) {
                        results.push(node);
                    }
                });
                return results;
            };

 #

            //背包问题
            function knapsack(capacity, size, value, n) {
                if (n == 0 || capacity == 0) {
                    return 0;
                }
                if (size[n - 1] > capacity) {
                    return knapsack(capacity, size, value, n - 1);
                } else {
                    return max(value[n - 1] +
                        knapsack(capacity - size[n - 1], size, value, n - 1),
                        knapsack(capacity, size, value, n - 1));
                }
            }

            function max(a, b) {
                return (a > b) ? a : b;
            }
            var value = [4, 5, 10, 11, 13];
            var size = [3, 4, 7, 8, 9];
            var capacity = 16;
            var n = 5;
            console.log(knapsack(capacity, size, value, n));

 

posted @ 2016-03-19 20:29  晴明桑  阅读(174)  评论(0编辑  收藏  举报