递归练习

//max值
function getMaxVal(arr){
    let maxVal=null;
    function loop(arr){
        let [key1,...keys]=arr;
        if(!key1){return}
        maxVal=key1>maxVal?key1:maxVal;
        loop(keys);
    }
    loop(arr)
    return maxVal;
}
getMaxVal([9,12,1,3,6])

function getMaxVal1(arr){
    function loop(arr){
        let [key1,...keys]=arr;
        if(!keys.length){return key1}
        return key1>loop(keys)?key1:loop(keys);
    }
    return loop(arr)
}
getMaxVal1([9,12,1,3,6])
let myReverse=(arr)=>{
    let [key1,...keys]=arr;
    if(!keys.length)return [key1]
    return [...myReverse(keys),key1]
}
myReverse(['a','b','c'])

 

面试题1: 楼梯问题
假如楼梯有n个台阶,每次可以走1个或2个台阶,请问走完这n个台阶有几种走法?
那如果有n个台阶呢?我们认真思考下就会发现,第1步的走法有两类:第一种是第一步走1个台阶,
第二种是第二步走2个台阶。所以n个台阶的走法就可以分为:走完1个台阶后的n
-1种走法,加上走完2个台阶后的n-2种走法,用递归公式表示就是: function climbStairs(n) { return climbStairs(n - 1) + climbStairs(n - 2) }

climbStairs(n) = climbStairs(n-1) + climbStairs(n-2)
climbStairs(n-1) = climbStairs(n-2) + climbStairs(n-3)
··· ··· ···
climbStairs(5) = climbStairs(4) + climbStairs(3)
climbStairs(4) = climbStairs(3) + climbStairs(2)
climbStairs(3) = climbStairs(2) + climbStairs(1)
climbStairs(2) = 2
climbStairs(1) = 1
function climbStairs(n) {
  if (n == 1) return 1
  if (n == 2) return 2
  return climbStairs(n-1) + climbStairs(n-2)
}

 参考:https://juejin.im/post/5d7df6bff265da03b638dc97

https://www.jb51.net/article/144663.htm

 

var temarr=
        [{
        "id": 1,
        "companyId": 33,
        "departmentId": 1,
        "name": "部门经理",
        "label": "部门经理",
        "code": "1001",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "",
        "updateTime": "2019-07-09 07:36:36",
        "createTime": "2019-06-26 08:15:56",
        "employeeCount": null,
        "children": [{
            "id": 103,
            "companyId": 33,
            "departmentId": 1,
            "name": "77777777",
            "label": "77777777",
            "code": "1234569999",
            "parentId": 1,
            "path": "1",
            "status": 1,
            "operationAccount": "liqisheng",
            "updateTime": "2019-07-25 08:46:09",
            "createTime": "2019-07-25 08:46:09",
            "employeeCount": null,
            "children": [{
                "id": 182,
                "companyId": 33,
                "departmentId": 1,
                "name": "fdaf",
                "label": "fdaf",
                "code": "88888",
                "parentId": 103,
                "path": "1,103",
                "status": 1,
                "operationAccount": "xiaochangliu",
                "updateTime": "2019-08-08 06:49:38",
                "createTime": "2019-08-08 06:49:38",
                "employeeCount": null,
                "children": [],
                "childCount": null,
                "parentName": null
            }],
            "childCount": null,
            "parentName": null
        }, {
            "id": 162,
            "companyId": 33,
            "departmentId": 1,
            "name": "bbbr",
            "label": "bbbr",
            "code": "bbbr1",
            "parentId": 1,
            "path": "1",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-31 06:06:56",
            "createTime": "2019-07-31 06:02:55",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 163,
            "companyId": 33,
            "departmentId": 1,
            "name": "fdsbb",
            "label": "fdsbb",
            "code": "fdsbb1",
            "parentId": 1,
            "path": "1",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-31 07:08:54",
            "createTime": "2019-07-31 07:08:54",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 220,
            "companyId": 33,
            "departmentId": 1,
            "name": "bumenpenpen",
            "label": "bumenpenpen",
            "code": "",
            "parentId": 1,
            "path": "1",
            "status": 1,
            "operationAccount": "wenfuzhou",
            "updateTime": "2019-08-30 07:25:23",
            "createTime": "2019-08-30 07:25:23",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 223,
            "companyId": 33,
            "departmentId": 1,
            "name": "penpen21123123",
            "label": "penpen21123123",
            "code": "",
            "parentId": 1,
            "path": "1",
            "status": 1,
            "operationAccount": "wenfuzhou",
            "updateTime": "2019-08-31 02:13:26",
            "createTime": "2019-08-31 02:13:26",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }],
        "childCount": null,
        "parentName": null
    }, {
        "id": 104,
        "companyId": 33,
        "departmentId": 1,
        "name": "88888",
        "label": "88888",
        "code": "335656",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "liqisheng",
        "updateTime": "2019-07-25 08:47:19",
        "createTime": "2019-07-25 08:47:19",
        "employeeCount": null,
        "children": [{
            "id": 113,
            "companyId": 33,
            "departmentId": 1,
            "name": "运营二哥",
            "label": "运营二哥",
            "code": "123123",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-27 09:00:45",
            "createTime": "2019-07-27 09:00:45",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 124,
            "companyId": 33,
            "departmentId": 1,
            "name": "000000pen",
            "label": "000000pen",
            "code": "2312312",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-29 11:20:02",
            "createTime": "2019-07-29 11:20:02",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 125,
            "companyId": 33,
            "departmentId": 1,
            "name": "11111111pen",
            "label": "11111111pen",
            "code": "",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-29 11:20:25",
            "createTime": "2019-07-29 11:20:25",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 126,
            "companyId": 33,
            "departmentId": 1,
            "name": "22222penguin",
            "label": "22222penguin",
            "code": "123123123123123",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-29 11:24:14",
            "createTime": "2019-07-29 11:24:14",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 127,
            "companyId": 33,
            "departmentId": 1,
            "name": "4444penguin",
            "label": "4444penguin",
            "code": "23423423",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-29 11:34:47",
            "createTime": "2019-07-29 11:34:47",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 128,
            "companyId": 33,
            "departmentId": 1,
            "name": "555555penguin",
            "label": "555555penguin",
            "code": "456456423423",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-29 11:36:33",
            "createTime": "2019-07-29 11:36:33",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 129,
            "companyId": 33,
            "departmentId": 1,
            "name": "6666penguin2312",
            "label": "6666penguin2312",
            "code": "",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-29 11:37:33",
            "createTime": "2019-07-29 11:37:33",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 130,
            "companyId": 33,
            "departmentId": 1,
            "name": "777777777penguin",
            "label": "777777777penguin",
            "code": "2344654756856",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-29 11:39:25",
            "createTime": "2019-07-29 11:39:25",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 131,
            "companyId": 33,
            "departmentId": 1,
            "name": "pentest007",
            "label": "pentest007",
            "code": "12312",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-29 12:33:50",
            "createTime": "2019-07-29 12:33:50",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 133,
            "companyId": 33,
            "departmentId": 1,
            "name": "pentttet3",
            "label": "pentttet3",
            "code": "12312213123123",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-29 12:46:25",
            "createTime": "2019-07-29 12:46:25",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 134,
            "companyId": 33,
            "departmentId": 1,
            "name": "000pentttet32133",
            "label": "000pentttet32133",
            "code": "12555555",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-29 12:48:07",
            "createTime": "2019-07-29 12:48:07",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 135,
            "companyId": 33,
            "departmentId": 1,
            "name": "testpen4445",
            "label": "testpen4445",
            "code": "21589111",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-29 12:49:03",
            "createTime": "2019-07-29 12:49:03",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 136,
            "companyId": 33,
            "departmentId": 1,
            "name": "121penger",
            "label": "121penger",
            "code": "2100000004234",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "sunxiukui",
            "updateTime": "2019-07-29 12:50:15",
            "createTime": "2019-07-29 12:50:15",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }, {
            "id": 137,
            "companyId": 33,
            "departmentId": 1,
            "name": "8989epenguin",
            "label": "8989epenguin",
            "code": "3999423423423",
            "parentId": 104,
            "path": "104",
            "status": 1,
            "operationAccount": "tangshiying",
            "updateTime": "2019-07-29 13:25:00",
            "createTime": "2019-07-29 12:51:19",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }],
        "childCount": null,
        "parentName": null
    }, {
        "id": 132,
        "companyId": 33,
        "departmentId": 1,
        "name": "pen000011222",
        "label": "pen000011222",
        "code": "1233123",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "sunxiukui",
        "updateTime": "2019-07-29 12:43:47",
        "createTime": "2019-07-29 12:36:05",
        "employeeCount": null,
        "children": [],
        "childCount": null,
        "parentName": null
    }, {
        "id": 146,
        "companyId": 33,
        "departmentId": 1,
        "name": "d就开始地方",
        "label": "d就开始地方",
        "code": "",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "sunxiukui",
        "updateTime": "2019-07-30 02:28:20",
        "createTime": "2019-07-30 02:28:20",
        "employeeCount": null,
        "children": [],
        "childCount": null,
        "parentName": null
    }, {
        "id": 147,
        "companyId": 33,
        "departmentId": 1,
        "name": "ddd对啊对啊",
        "label": "ddd对啊对啊",
        "code": "",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "sunxiukui",
        "updateTime": "2019-07-30 07:57:04",
        "createTime": "2019-07-30 02:28:44",
        "employeeCount": null,
        "children": [],
        "childCount": null,
        "parentName": null
    }, {
        "id": 151,
        "companyId": 33,
        "departmentId": 1,
        "name": "45发送到发送到45",
        "label": "45发送到发送到45",
        "code": "",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "xiaochangliu",
        "updateTime": "2019-08-05 09:16:39",
        "createTime": "2019-07-30 08:49:01",
        "employeeCount": null,
        "children": [],
        "childCount": null,
        "parentName": null
    }, {
        "id": 154,
        "companyId": 33,
        "departmentId": 1,
        "name": "fsdfsd.防守打法",
        "label": "fsdfsd.防守打法",
        "code": "",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "sunxiukui",
        "updateTime": "2019-07-30 08:58:13",
        "createTime": "2019-07-30 08:58:13",
        "employeeCount": null,
        "children": [],
        "childCount": null,
        "parentName": null
    }, {
        "id": 164,
        "companyId": 33,
        "departmentId": 1,
        "name": "fsdfsd",
        "label": "fsdfsd",
        "code": "fsdfsd1",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "sunxiukui",
        "updateTime": "2019-07-31 07:13:54",
        "createTime": "2019-07-31 07:13:54",
        "employeeCount": null,
        "children": [],
        "childCount": null,
        "parentName": null
    }, {
        "id": 165,
        "companyId": 33,
        "departmentId": 1,
        "name": "fsdfsdfsd",
        "label": "fsdfsdfsd",
        "code": "",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "sunxiukui",
        "updateTime": "2019-07-31 07:20:21",
        "createTime": "2019-07-31 07:20:21",
        "employeeCount": null,
        "children": [],
        "childCount": null,
        "parentName": null
    }, {
        "id": 166,
        "companyId": 33,
        "departmentId": 1,
        "name": "fsdfsd23123",
        "label": "fsdfsd23123",
        "code": "eqweqw",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "sunxiukui",
        "updateTime": "2019-07-31 07:24:12",
        "createTime": "2019-07-31 07:24:12",
        "employeeCount": null,
        "children": [],
        "childCount": null,
        "parentName": null
    }, {
        "id": 178,
        "companyId": 33,
        "departmentId": 1,
        "name": "fdafda",
        "label": "fdafda",
        "code": "fda",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "xiaochangliu",
        "updateTime": "2019-08-08 03:20:14",
        "createTime": "2019-08-08 03:20:14",
        "employeeCount": null,
        "children": [],
        "childCount": null,
        "parentName": null
    }, {
        "id": 179,
        "companyId": 33,
        "departmentId": 1,
        "name": "gfds",
        "label": "gfds",
        "code": "gfsgfsd",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "xiaochangliu",
        "updateTime": "2019-08-08 03:24:41",
        "createTime": "2019-08-08 03:24:41",
        "employeeCount": null,
        "children": [],
        "childCount": null,
        "parentName": null
    }, {
        "id": 180,
        "companyId": 33,
        "departmentId": 1,
        "name": "fdsa",
        "label": "fdsa",
        "code": "f",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "xiaochangliu",
        "updateTime": "2019-08-08 06:20:47",
        "createTime": "2019-08-08 06:20:47",
        "employeeCount": null,
        "children": [],
        "childCount": null,
        "parentName": null
    }, {
        "id": 184,
        "companyId": 33,
        "departmentId": 1,
        "name": "fdafdas",
        "label": "fdafdas",
        "code": "464646",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "xiaochangliu",
        "updateTime": "2019-08-08 06:53:04",
        "createTime": "2019-08-08 06:53:04",
        "employeeCount": null,
        "children": [],
        "childCount": null,
        "parentName": null
    }, {
        "id": 194,
        "companyId": 33,
        "departmentId": 1,
        "name": "运营岗1",
        "label": "运营岗1",
        "code": "",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "liqisheng",
        "updateTime": "2019-08-13 07:40:34",
        "createTime": "2019-08-13 07:40:02",
        "employeeCount": null,
        "children": [{
            "id": 224,
            "companyId": 33,
            "departmentId": 1,
            "name": "运营岗刚刚",
            "label": "运营岗刚刚",
            "code": "",
            "parentId": 194,
            "path": "194",
            "status": 1,
            "operationAccount": "wenfuzhou",
            "updateTime": "2019-09-02 06:20:31",
            "createTime": "2019-09-02 06:20:31",
            "employeeCount": null,
            "children": [],
            "childCount": null,
            "parentName": null
        }],
        "childCount": null,
        "parentName": null
    }, {
        "id": 232,
        "companyId": 33,
        "departmentId": 1,
        "name": "-._user123",
        "label": "-._user123",
        "code": "",
        "parentId": 0,
        "path": "",
        "status": 1,
        "operationAccount": "liqisheng",
        "updateTime": "2019-09-12 15:05:16",
        "createTime": "2019-09-12 15:05:16",
        "employeeCount": null,
        "children": [],
        "childCount": null,
        "parentName": null
    }]
var oneDimensionArr2=function(arr){
    return arr.reduce((pre,cur)=>{
        if(!cur.children)return[]
        return (Array.isArray(cur.children)&&cur.children.length)?[...pre,cur,...this.oneDimensionArr2(cur.children)]:[...pre,cur]
    },[])
}

 

posted @ 2019-09-15 15:43  little_ab  阅读(278)  评论(0编辑  收藏  举报