Iterative solution for flattening n-th nested arrays in Javascript

后端 未结 8 1084
猫巷女王i
猫巷女王i 2021-02-04 12:46

Can anyone show me an iterative solution for the following problem? I solved it recursively but struggled with an iterative solution. (Facebook Technical Interv

8条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-04 13:48

    function flatten(array){
      for(var i=0;i

    This in-place solution is faster than Lupe's, now that I've removed all of the inner curly brackets (I inlined the i-- in the concat parameter to do that).

提交回复
热议问题