ios 面试题代码篇_iOS面试准备指南快速一周中的30天代码挑战5 5

ios 面试题代码篇

This is the last week of the 30-Day Code Challenge. I hope you feel your iOS and Swift skills have extended to the next level. Anyway, good job to those developers sticking around this.

这是30天代码挑战赛的最后一周。 我希望您觉得您的iOS和Swift技能已扩展到新的水平。 无论如何,对于坚持这一点的那些开发人员来说,是一项好工作。

If you miss the articles for the previous weeks, here’s the compilation of them — Week 1, Week 2, Week 3, and Week 4. All solutions and unit tests are up to my Github repo, feel free to use them for your interview preparation.

如果您错过了前几周的文章,请参考以下文章的汇编- 第1 第2 第3 第4周 。 所有解决方案和单元测试均由我的Github存储库决定 ,可以随时将它们用于面试准备。

The following are the remaining 2 coding questions of the challenge that we are going to cover in this article.

以下是我们将在本文中讨论的挑战的其余两个编码问题。

  1. Binary Tree Maximum Path Sum (Solution) 🥇

    二叉树最大路径总和 ( )🥇

  2. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree (Solution) 🥈

    检查字符串是否是二叉树中从根到叶子路径的有效序列 ( 解决方案 )🥈

(🥇 🥈 indicate writer’s selection of high-frequency interview questions.)

( 🥈 表示作者选择的高频访谈问题。)

二叉树最大路径总和 (Binary Tree Maximum Path Sum)

🥇 Writer’s selection of the week

🥇 作家的每周精选

Description: Given a non-empty binary tree, find the maximum path sum. A path is defined as any sequence of nodes from some starting node to any node in the tree along with the parent-child connections. The path must contain at least one node and does not need to go through the root.

描述 :给定一个非空的二叉树,找到最大路径总和。 路径定义为从某个起始节点到树中任何节点的任何节点序列以及父子连接。 该路径必须至少包含一个节点并且不需要通过根。

Example 1Input: [1, 2, 3]       1/ \2   3Output: 6Example 2Input: [-10, 9, 20, null, null, 15, 7]   -10
/ \
9 20/ \15 7Output: 42

This question looks like an extended version of another question — Diameter of Binary Tree from Week 2. I suggest you take a look at it if you haven’t done so.

这个问题看起来像是另一个问题的扩展版本- 第二周的二叉树直径 。 如果您还没有这样做的话,建议您看看。

The major difference is that the previous question was asking path with the longest length while this one is asking the path with maximal value. Similar to the previous one, we can divide the problem into three subproblems.

主要的区别是,以前的问题是问最长的长度路径,而这一个是要求具有最大 价值的路径。 与上一个问题类似,我们可以将问题分为三个子问题。

  1. How to calculate a one-way path that has the largest value? Let’s define a leg as the one-way path that does not pass the local root, which is the starting node passed to the recursion.

    如何计算具有最大价值的单向路径? 让我们将一条定义为通过本地根的单向路径,该根是传递到递归的起始节点。

  2. How to find the local max-value path of a node? It consists of up to two non-overlapping legs since it may or may not pass the root. That said, legs that share the same root may not be included simultaneously.

    如何找到节点的局部 最大值路径 ? 它由最多两个不重叠的分支组成,因为它可能会通过根,也可能不会通过根。 也就是说,共享同一根的腿可能不会同时包含在内。

  3. How to find the max-value path of the entire tree? It will be obtained by comparing the local max-value paths.

    如何找到整个树的最大值路径 ? 将通过比较局部最大值路径获得

First of all, we want to find a way to calculate the maximum value of each leg. In Example 2, the root has three possible legs, which are [9], [20, 15], and [20, 7]. Since a path can have up to two non-overlapping legs, we will choose [9] and[20, 15] to maximize the value.

首先,我们想找到一种方法来计算每条腿的最大值。 在示例2中 ,根具有三个可能的分支,分别是[9][20,15][20,7] 。 由于一条路径最多可以包含两个不重叠的 分支 ,因此我们将选择[9][20,15]来最大化该值。

To calculate the local max-value starting from the root node, we simply add up the values of the selected legs, which sum to 34. (-10 + 9 + 20 + 15 = 34)

要从根节点开始计算局部 最大值 ,我们只需将所选支路的值相加即可,总计为34。 (-10 + 9 + 20 + 15 = 34 )

Moving on to the next recursion, we want to also find the local max-value (LMV) starting from the left and right node of the root. The left LMV is obviously 9. Apart from that, since the right node only has two legs, we can get its LMV by merely adding up the values of those legs to the right node’s value, which then becomes 42. (20 + 15 + 7 = 42)

移动到下一个递归,我们也想找到当地 最大值(LMV)从根的节点开始。 左LMV显然为9 。 除此之外,由于节点仅具有 两条腿,我们只需将这些腿的值与右节点的值相加即可得到LMV ,然后变为42 。 (20 + 15 + 7 = 42 )

By now, we have traversed through the entire tree, therefore, we’ve got all the LMVs. We can get the final max-value path of the tree by comparing them. In our case, the maximum value we can get from all paths is 42. Note that this comparison process is continuously executed in each recursion every time we calculate a new LMV.

至此,我们遍历了整个树,因此,我们拥有了所有LMV 。 我们可以通过比较它们来获得树的最终最大值路径。 在我们的例子中,我们从所有路径中可以获得的最大值是42 。 请注意,每次我们计算新的LMV时,此比较过程都会在每次递归中连续执行。

Following the solutions to the subproblems illustrated above, we can come out with a recursive solution as below. The explanation of each subproblem is embedded in comments.

按照上面说明的子问题的解决方案,我们可以得出如下的递归解决方案。 每个子问题的解释都包含在注释中。

Time complexity: O(N) as we go through the entire tree. Each node is counted once for the local max-value path.

时间复杂度:遍历整个树时为O(N) 。 每个节点对本地 最大值路径计数一次。

检查字符串是否是二叉树中从根到叶子路径的有效序列 (Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree)

🥈 Writer’s selection of the week

🥈 作家的每周精选

Description: Given a binary tree where each path going from the root to any leaf form a valid sequence, check if a given string is a valid sequence in such a binary tree.

描述 :给定一个二叉树,其中从根到任何叶子的每条路径都形成一个有效序列 ,请检查给定的字符串在这种二叉树中是否为有效序列

We get the given string from the concatenation of an array of integers arr and the concatenation of all values of the nodes along a path results in a sequence in the given binary tree.

我们从整数数组arr的串联中获得给定的字符串,沿着路径的节点的所有值的串联导致给定的二叉树中的序列

Example 1Input 1 - tree: [0, 1, 0, 0, 1, 0, null, null, 1, 0, 0]        0
/ \
1 0
/ \ /
0 1 0
\ / \
1 0 0
Input 2 - arr = [0, 1, 0, 1]Output: true---------------------------------------------Example 2Input 1 - tree: [0, 1, 0, 0, 1, 0, null, null, 1, 0, 0] 0
/ \
1 0
/ \ /
0 1 0
\ / \
1 0 0
Input 2 - arr = [0, 0, 1]Output: false---------------------------------------------Example 3Input 1 - tree: [0, 1, 0, 0, 1, 0, null, null, 1, 0, 0] 0
/ \
1 0
/ \ /
0 1 0
\ / \
1 0 0
Input 2 - arr = [0, 1, 1]Output: false

Here comes another tree question for the week. In this question, all we have to do is traverse the entire tree while passing an array to see if it exists in the tree. In order to do the job efficiently, we can use a backtracking technique while traversing the tree.

这是本周的另一个问题。 在这个问题中,我们要做的就是遍历整棵树,同时传递一个数组以查看它是否存在于树中。 为了有效地完成这项工作,我们可以在遍历树时使用回溯技术。

Each time we traverse to a node in the tree, we add the value of that node to the backtracking array. Thereafter, when there is no child found from the current node, we compare the backtracking array to the target array.

每次我们遍历树中的一个节点时,都会将该节点的值添加到回溯数组中。 此后,当从当前节点中找不到子节点时,我们将回溯数组与目标数组进行比较。

Time complexity: O(N) as we traverse through the entire tree.

时间复杂度:遍历整棵树时为O(N)

Congratulations! We have completed the 30-day Swift code challenge. It is a good first step to prepare your next iOS technical interview. Materials of the April code challenges are all up in my Github. Besides, I plan to publish another series of 10–15 questions selected from the May LeetCoding challenge. Meanwhile, feel free to ask any questions you may have.

恭喜你! 我们已经完成了30天的Swift代码挑战。 这是准备下一次iOS技术面试的第一步。 我的Github中充斥着四月份代码挑战的资料。 此外,我计划发布从May LeetCoding挑战赛中选出的另外10-15个问题系列。 同时,随时问您任何问题。

翻译自: https://medium.com/swlh/ios-interview-prep-guide-30-day-code-challenge-in-swift-week-5-5-d093e2cd4217

ios 面试题代码篇

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值