遇到的面试题
Given an array that may contain nested arrays, return a flattened array. Input and out put are illustrated as follows.
将含有嵌套的数组排序输出。*号部分为需要写出的代码。
var input = [{a: 'a'}, 'b', ['c', 'd'], ['e', ['f']], 'g'];
function flatten_array(arr){
var out = [];
*******;
return out;
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
楼上的代码 不是很好(太长自己实在懒得看) 这个题目很明显应该用递归解决:(修改感谢@felix021提醒...)
再有 关于1楼 如果不是自己解决的 请不要乱贴代码 乱贴只会误导别人.
扁平的数组额。。曾经在群里看到过司徒正美大大的题目。感觉应该是差不多。
功能为
传入 ['a','b',['c','d']]
会返回 [ ['a','b'], ['a','b','c','d']]
传入 [ 'a', 'b', [ 'c', [ 'd' ] ] ]
会返回 [ [ 'a', 'b', 'c', 'd' ], [ 'a', 'b', 'c' ], [ 'a', 'b' ] ]
传入[ 'a', 'b', [ 'c', [ 'd' ], 'e', [ 'f' ] ] ]
会返回
本人不才,引用下大神本人的解答
代码版权归司徒正美本人.
其实我也没怎么研究过他的这段代码