We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
map
forEach
every
some
题目
二叉树的最大深度
代码
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {number} */ var maxDepth = function(root) { if (!root) return 0; return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); };
思路: 对于某个节点,其最大深度为自身 1 加上 其左节点和右节点二者的深度最大值。 而左节点或右节点也可以作为某节点,故形成递归,如上代码所示。 每个节点都会遍历到一次,故时间复杂度 = O(n);不占用额外空间,故空间复杂度 = O(1)。
对比: 与高分对比: 本代码运行 39 个测试用例花费约 92ms,平均一个测试用例约 2.4ms; 高分代码运行 39 个测试用例花费约 92ms,平均一个测试用例约 2.4ms。 递归思路一致代码略。 官方还提供了迭代思路和代码。 详见LeetCode 题解 | 104. 二叉树的最大深度。
阅读: Introduction to Elasticsearch Using Node.js—Part 1
点评: 本文介绍了 ElasticSearch 的几个核心概念:
扩展阅读:
数组遍历的方法除了使用 for 循环,还可以使用函数式编程的 map、forEach、every、some 等 除了 for 循环,其它都不能使用 break 来中止遍历,使用 throw Error 的方法又差强人意
for
map 和 forEach 中止遍历的方法:修改数组
var array = [1, 2, 3, 4, 5]; array.map(function(item, index) { if (item === 2) { array = array.concat(array.splice(index, array.length - index)); } console.log(item); //只输出1,2 }); array.forEach(function(item, index) { if (item === 2) { array = array.concat(array.splice(index, array.length - index)); } console.log(item); //只输出1,2 });
every 和 some 中止遍历的方法:every false、some true
every false
some true
every 碰到 return false 的时候,遍历中止 some 碰到 return ture 的时候,遍历中止
return false
return ture
var a = [1, 2, 3, 4, 5]; a.every(function(item, index, arry) { console.log(item); //返回1,2 if (item === 2) { return false } else { return true } }) a.some(function(item, index, arry) { console.log(item); //返回1,2 if (item === 2) { return true } else { return false } })
参考:如何在Array.forEach的循环里break
分享一篇极客“左耳听风”专栏文章(据说可限10位免费阅读哦) 25 | 分布式系统的关键技术:服务调度
The text was updated successfully, but these errors were encountered:
No branches or pull requests
ARTS 第十一周(2019.9.9~2019.9.15)
map
、forEach
、every
、some
过程Algorithm 二叉树的最大深度
题目
二叉树的最大深度
代码
思路:
对于某个节点,其最大深度为自身 1 加上 其左节点和右节点二者的深度最大值。
而左节点或右节点也可以作为某节点,故形成递归,如上代码所示。
每个节点都会遍历到一次,故时间复杂度 = O(n);不占用额外空间,故空间复杂度 = O(1)。
对比:
与高分对比:
本代码运行 39 个测试用例花费约 92ms,平均一个测试用例约 2.4ms;
高分代码运行 39 个测试用例花费约 92ms,平均一个测试用例约 2.4ms。
递归思路一致代码略。
官方还提供了迭代思路和代码。
详见LeetCode 题解 | 104. 二叉树的最大深度。
Review Node.js 使用 Elasticsearch 入门(上)
阅读:
Introduction to Elasticsearch Using Node.js—Part 1
点评:
本文介绍了 ElasticSearch 的几个核心概念:
(文末有图文示例)
扩展阅读:
Tip 中止数组的
map
、forEach
、every
、some
过程数组遍历的方法除了使用
for
循环,还可以使用函数式编程的map
、forEach
、every
、some
等除了
for
循环,其它都不能使用 break 来中止遍历,使用 throw Error 的方法又差强人意map
和forEach
中止遍历的方法:修改数组every
和some
中止遍历的方法:every false
、some true
every
碰到return false
的时候,遍历中止some
碰到return ture
的时候,遍历中止参考:如何在Array.forEach的循环里break
Share [极客专栏] 25 | 分布式系统的关键技术:服务调度
分享一篇极客“左耳听风”专栏文章(据说可限10位免费阅读哦)
25 | 分布式系统的关键技术:服务调度
The text was updated successfully, but these errors were encountered: