笔试时间为2019年4月14日,五道编程题。编程题如下:
1.变身程序员
题目描述
公司的程序员不够用了,决定把产品经理都转变为程序员以解决开发时间长的问题。
在给定的矩形网格中,每个单元格都可以用以下三个值之一:
- 值0代表空单元格
- 值1代表产品经理
- 值2代表程序员
每分钟,任何与程序员(在四个正方向上)相邻的产品经理都会变成程序员。
返回知道单元格中没有产品经理为止所必须经过的最小分钟数。
如果没有可能,返回-1.
人生苦短,我用Python
You are given a series of video clips from a sporting event that lasted T seconds. These video clips can be overlapping with each other and have varied lengths.
Each video clip clips[i]
is an interval: it starts at time clips[i][0]
and ends at time clips[i][1]
. We can cut these clips into segments freely: for example, a clip [0, 7]
can be cut into segments [0, 1] + [1, 3] + [3, 7]
.
Return the minimum number of clips needed so that we can cut the clips into segments that cover the entire sporting event ([0, T]
). If the task is impossible, return -1
.
你将会获得一系列视频片段,这些片段来自于一项持续时长为 T 秒的体育赛事。这些片段可能有所重叠,也可能长度不一。
视频片段 clips[i]
都用区间进行表示:开始于 clips[i][0]
并于 clips[i][1]
结束。我们甚至可以对这些片段自由地再剪辑,例如片段 [0, 7]
可以剪切成 [0, 1] + [1, 3] + [3, 7]
三部分。
我们需要将这些片段进行再剪辑,并将剪辑后的内容拼接成覆盖整个运动过程的片段([0, T]
)。返回所需片段的最小数目,如果无法完成该任务,则返回 -1
。
题目链接:https://leetcode.com/problems/video-stitching/
A query word matches a given pattern
if we can insert lowercase
letters to the pattern word so that it equals the query
. (We may insert each character at any position, and may insert 0 characters.)
Given a list of queries
, and a pattern, return an answer
list of booleans, where answer[i]
is true if and only if queries[i]
matches the pattern.
如果我们可以将小写字母插入模式串 pattern
得到待查询项 query
,那么待查询项与给定模式串匹配。(我们可以在任何位置插入每个字符,也可以插入 0 个字符。)
给定待查询列表 queries
,和模式串 pattern
,返回由布尔值组成的答案列表 answer
。只有在待查项 queries[i]
与模式串 pattern
匹配时, answer[i]
才为 true
,否则为 false
。
题目链接:https://leetcode.com/problems/camelcase-matching/
Given a binary tree, each node has value 0
or 1
. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1
, then this could represent 01101
in binary, which is 13
.
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.
Return the sum of these numbers.
给出一棵二叉树,其上每个结点的值都是 0
或 1
。每一条从根到叶的路径都代表一个从最高有效位开始的二进制数。例如,如果路径为 0 -> 1 -> 1 -> 0 -> 1
,那么它表示二进制数 01101
,也就是 13
。
对树上的每一片叶子,我们都要找出从根到该叶子的路径所表示的数字。
以 10^9 + 7
为模,返回这些数字之和。
题目链接:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/
A valid parentheses string is either empty ("")
, "(" + A + ")"
, or A + B
, where A
and B
are valid parentheses strings, and + represents string concatenation. For example, ""
, "()"
, "(())()"
, and "(()(()))"
are all valid parentheses strings.
A valid parentheses string S
is primitive if it is nonempty, and there does not exist a way to split it into S = A+B
, with A
and B
nonempty valid parentheses strings.
Given a valid parentheses string S
, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k
, where P_i
are primitive valid parentheses strings.
Return S
after removing the outermost parentheses of every primitive string in the primitive decomposition of S
.
有效括号字符串为空 ("")
、"(" + A + ")"
或 A + B
,其中 A
和 B
都是有效的括号字符串,+ 代表字符串的连接。例如,""
,"()"
,"(())()"
和 "(()(()))"
都是有效的括号字符串。
如果有效字符串 S
非空,且不存在将其拆分为 S = A+B
的方法,我们称其为原语(primitive),其中 A
和 B
都是非空有效括号字符串。
给出一个非空有效字符串 S
,考虑将其进行原语化分解,使得:S = P_1 + P_2 + ... + P_k
,其中 P_i
是有效括号字符串原语。
对 S
进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 S
。
题目链接:https://leetcode.com/problems/remove-outermost-parentheses/
Given a 2D array A
, each cell is 0 (representing sea) or 1 (representing land)
A move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid.
Return the number of land squares in the grid for which we cannot walk off the boundary of the grid in any number of moves.
给出一个二维数组 A
,每个单元格为 0(代表海)或 1(代表陆地)。
移动是指在陆地上从一个地方走到另一个地方(朝四个方向之一)或离开网格的边界。
返回网格中无法在任意次数的移动中离开网格边界的陆地单元格的数量。
题目链接:https://leetcode.com/problems/number-of-enclaves/
We are given a linked list with head
as the first node. Let’s number the nodes in the list: node_1
, node_2
, node_3
, … etc.
Each node may have a next larger value: for node_i
, next_larger(node_i)
is the node_j.val
such that j > i
, node_j.val > node_i.val
, and j
is the smallest possible choice. If such a j
does not exist, the next larger value is 0
.
Return an array of integers answer, where answer[i] = next_larger(node_{i+1})
.
Note that in the example inputs
(not outputs) below, arrays such as [2,1,5]
represent the serialization of a linked list with a head node value of 2, second node value of 1, and third node value of 5.
给出一个以头节点 head
作为第一个节点的链表。链表中的节点分别编号为:node_1
, node_2
, node_3
, … 。
每个节点都可能有下一个更大值(next larger value
):对于 node_i
,如果其 next_larger(node_i)
是 node_j.val
,那么就有 j > i
且 node_j.val > node_i.val
,而 j
是可能的选项中最小的那个。如果不存在这样的 j
,那么下一个更大值为 0
。
返回整数答案数组 answer
,其中 answer[i] = next_larger(node_{i+1})
。
注意:在下面的示例中,诸如 [2,1,5]
这样的输入(不是输出)是链表的序列化表示,其头节点的值为 2,第二个节点值为 1,第三个节点值为 5 。
题目链接:https://leetcode.com/problems/next-greater-node-in-linked-list/
Given an array A
of 0s
and 1s
, consider N_i
: the i-th subarray from A[0]
to A[i]
interpreted as a binary number (from most-significant-bit to least-significant-bit.)
Return a list of booleans answer
, where answer[i]
is true
if and only if N_i
is divisible by 5
.
给定由若干 0
和 1
组成的数组 A
。我们定义 N_i
:从 A[0]
到 A[i]
的第 i
个子数组被解释为一个二进制数(从最高有效位到最低有效位)。
返回布尔值列表 answer
,只有当 N_i
可以被 5
整除时,答案 answer[i]
为 true
,否则为 false
。
题目链接:https://leetcode.com/problems/binary-prefix-divisible-by-5/
Given a number N
, return a string consisting of “0”s and “1”s that represents its value in base -2
(negative two).
The returned string must have no leading zeroes, unless the string is “0”.
给出数字 N
,返回由若干 “0” 和 “1”组成的字符串,该字符串为 N
的负二进制(base -2)表示。
除非字符串就是 “0”,否则返回的字符串中不能含有前导零。
题目链接:https://leetcode.com/problems/convert-to-base-2/