美文网首页
循环转递归

循环转递归

作者: Sharise_Mo佩珊 | 来源:发表于2017-02-24 18:59 被阅读0次

    递归算法应用场景:http://www.cnblogs.com/handsCool/p/4496495.html

    • 递归需要边界条件,递归前进段,递归返回段
    • 递归条件不成立 ==》 递归前进
    • 递归条件满足 ==》 递归返回
      循环 实现阶乘 和 递归 实现 一个版本出来
    • 阶乘:输入 n! = n * (n-1) * (n-2) ..... 1
    function factorial(num){
        if(num<1){
            return 1;
        }else{
            return num*factorial(num-1);
        }
    }
    
    
    • n阶楼梯,小张每次只能走一阶或者两阶,请问走完此楼梯共有多少种方法?
     function fun(s){
          if(s==0 || s==1){
            return 1;
          }else{
          return fun(s-1)+fun(s-2);
          }
     }
    
     console.log(fun(4));
    
    <select class="agt_rt_nm_ipt province" style="width: 270px" name="provinceId" data-provinceid="{{provinceId}}"></select>
    <select class="agt_rt_nm_ipt city" style="width: 270px" name="cityId" data-cityid="{{cityId}}"></select>
    <select class="agt_rt_nm_ipt district" style="width: 270px" name="areaId" data-areaid="{{areaId}}"></select>
    <select class="agt_rt_nm_ipt street" style="width: 270px" name="streetsId" data-streestid="{{streetsId}}"></select>
    
     function areaSelect(province,city,district,street){
            var html = "<option>省</option>";
            $(city).append("<option>市</option>");
            $(district).append("<option>区</option>");
            $(street).append("<option>街道</option>");
            for(var i = 0;i<areaList.length;i++){
                var pp = areaList[i];
                html += "<option value='"+pp['id']+"' code='"+pp['code']+"' parentid='"+pp['parentId']+"'>"+pp['name']+"</option>";
            }
            $(province).html(html);
    
            $(province).change(function(){
                if($(this).val()=="")  return;
                $(city+' option').remove();
                $(district+' option').remove();
                $(street+' option').remove();
                var code = $(this).find('option:selected').attr("code");
                var html = "<option>市</option>";
                for(var i = 0;i<areaList.length;i++){
                    for(var j = 0;j<areaList[i]['subordinates'].length;j++){
                        var cc = areaList[i]['subordinates'][j];
                        if(cc['parentId'] == code){
                            html += "<option value='"+cc['id']+"' code='"+cc['code']+"' parentid='"+cc['parentId']+"'>"+cc['name']+"</option>";
                        }
                    }
                }
                $(city).html(html);
            });
    
            $(city).change(function(){
                if($(this).val()=="") return;
                $(district+' option').remove();
                $(street+' option').remove();
                var code = $(this).find('option:selected').attr("code");
                var html = "<option>区</option>";
                for(var i = 0;i<areaList.length;i++){
                    for(var j = 0;j<areaList[i]['subordinates'].length;j++){
                        for(var k = 0;k<areaList[i]['subordinates'][j]['subordinates'].length;k++){
                            var dd = areaList[i]['subordinates'][j]['subordinates'][k];
                            if(dd['parentId'] == code){
                                html += "<option value='"+dd['id']+"' code='"+dd['code']+"' parentid='"+dd['parentId']+"'>"+dd['name']+"</option>";
                            }
                        }
                    }
                }
                $(district).html(html);
            });
    
            $(district).change(function(){
                if($(this).val()=="") return;
                $(street+' option').remove();
                var code = $(this).find('option:selected').attr("code");
                var html = "<option>街道</option>";
                for(var i = 0;i<areaList.length;i++){
                    for(var j = 0;j<areaList[i]['subordinates'].length;j++){
                        for(var k = 0;k<areaList[i]['subordinates'][j]['subordinates'].length;k++){
                            for(var l = 0;l<areaList[i]['subordinates'][j]['subordinates'][k]['subordinates'].length;l++){
                                var tt = areaList[i]['subordinates'][j]['subordinates'][k]['subordinates'][l];
                                if(tt['parentId'] == code){
                                    html +=  "<option value='"+tt['id']+"' code='"+tt['code']+"' parentid='"+tt['parentId']+"'>"+tt['name']+"</option>";
                                }
                            }
                        }
                    }
                }
                $(street).html(html);
            });
        }
        areaSelect(".province",".city",".district",".street");
    
    
        function selectOption(selector){
            var _selectorId = $("select[name="+selector+"]").attr("data-"+selector);
            var options = $("select[name="+selector+"]").children("option");
            for(var i =0;i<options.length;i++){
                if(options[i].value == _selectorId){
                    options[i].selected = true;
                }
            } 
        }
    
        selectOption("provinceId");
        selectOption("cityId");
        selectOption("areaId");
        selectOption("streetsId");
    

    闭包【closure】

    应用场景

    • 闭包常用来实现对象的私有数据,在事件处理和回调函数中会用到
    • 偏函数,柯里化,函数式编程等

    什么是闭包

    • 闭包能让我们从一个函数内部访问其外部函数的作用域【内部函数能访问外部函数作用域中的变量】
    • 在JS中,闭包是一种用来实现数据私有的原生机制,当使用闭包来实现数据私有时,被封装的变量只有在闭包容器函数作用域中使用。在闭包作用域下的公开方法才可以访问这些数据。
    • 闭包通常是在一个函数内部创建另一个函数
    • 闭包:一个函数可以访问不在其作用域范围内但在其外层作用域中存在的变量,该外层作用域的顶层为全局作用域
    • 闭包不等于匿名函数
    • 闭包的目的是通过返回函数来扩大作用域

    相关文章

      网友评论

          本文标题:循环转递归

          本文链接:https://www.haomeiwen.com/subject/psgwwttx.html