算法题(含初中高各等级)

1.[Easy]Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
给定一个数字和数字列表 k,还从列表中是否有两个数字加起来 k. 例如,给定 [10, 15, 3, 7]和 k的 17以来,还真 10 + 7是 17.

2.[Hard]Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
给定一个整数数组,返回一个新数组,每个元素索引 i新数组的所有原始数组中的数字的产品除了一个 i. 例如,如果我们的输入 [1, 2, 3, 4, 5]预期的输出 [120, 60, 40, 30, 24]。如果我们的输入 [3, 2, 1]预期的输出 [2, 3, 6].

3.[Medium] Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.
给定一个二叉树的根,实现 serialize(root),树序列化成字符串 deserialize(s)回到树,反序列化字符串。
For example, given the following Node class
class Node:
def init(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
The following test should pass:
node = Node(‘root’, Node(‘left’, Node(‘left.left’)), Node(‘right’))
assert deserialize(serialize(node)).left.left.val == ‘left.left’

4.[Hard] Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.
For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.
给定一个整数数组,找到第一个失踪的正整数在线性时间和恒定的空间。换句话说,找到最低的正整数数组中不存在。数组可以包含重复和负数。 例如,输入 [3, 4, -1, 1]应该给 2。输入 [1, 2, 0]应该给 3.

5.[Medium] cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4.
cons(a, b)构造一对 car(pair)和 cdr(pair)返回的第一个和最后一个元素。例如, car(cons(3, 4))返回 3, cdr(cons(3, 4))返回 4.
Given this implementation of cons:
def cons(a, b):
def pair(f):
return f(a, b)
return pair
Implement(实现) car and cdr.

6.[Hard] An XOR linked list is a more memory efficient doubly linked list. Instead of each node holding next and prev fields, it holds a field named both, which is an XOR of the next node and the previous node. Implement an XOR linked list; it has an add(element) which adds the element to the end, and a get(index) which returns the node at index.
If using a language that has no pointers (such as Python), you can assume you have access to get_pointer and dereference_pointer functions that converts between nodes and memory addresses.
异或链表是一种高效的双向链表内存更大的内存。而不是每个节点 next和 prev字段,该字段命名 both是下一个节点的XOR和前面的节点。实现一个异或链表;它有一个 add(element)将元素添加到最后,和 get(index)返回节点的索引。 如果使用一种语言,没有指针(如Python),你可以假设你有访问权 get_pointer和 dereference_pointer功能节点和内存地址之间的转换

7.[Medium]Given the mapping a = 1, b = 2, … z = 26, and an encoded message, count the number of ways it can be decoded.
For example, the message ‘111’ would give 3, since it could be decoded as ‘aaa’, ‘ka’, and ‘ak’.
You can assume that the messages are decodable. For example, ‘001’ is not allowed.
考虑到映射a = 1, b = 2,…z = 26日和一个编码信息,数一数它可以解码的方法。 例如,“111”的消息会给3,因为它可以解码为“aaa”,“卡”,“正义与发展”。 你可以假设消息解码了。例如,“001”是不允许的。

8.[Easy]A unival tree (which stands for “universal value”) is a tree where all nodes under it have the same value.
Given the root to a binary tree, count the number of unival subtrees.
For example, the following tree has 5 unival subtrees:
unival树(即“普世价值”)是一个树下所有节点有相同的值。 二叉树的根,计算unival子树的数量。 例如,下面的树有5 unival子树:
0
/
1 0
/
1 0
/
1 1

9.[Hard] Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. Numbers can be 0 or negative.
For example, [2, 4, 6, 2, 5] should return 13, since we pick 2, 6, and 5. [5, 1, 1, 5] should return 10, since we pick 5 and 5.
Follow-up: Can you do this in O(N) time and constant space?
给定一个整数列表,写一个函数,它返回来最大的和不相邻的数字。数字可以 0或消极。 例如, [2, 4, 6, 2, 5]应该返回 13,因为我们选择 2, 6, 5. [5, 1, 1, 5]应该返回 10,因为我们选择 5和 5. 追问:你能在O (N)时间和恒定的空间吗?

10.[Medium] Implement a job scheduler which takes in a function f and an integer n, and calls f after n milliseconds.
实现一个作业调度程序将在一个函数 f和一个整数 n,并调用 f后 n毫秒。

11.[Medium]Implement an autocomplete system. That is, given a query string s and a set of all possible query strings, return all strings in the set that have s as a prefix.
For example, given the query string de and the set of strings [dog, deer, deal], return [deer, deal].
Hint: Try preprocessing the dictionary into a more efficient data structure to speed up queries.
实现一个系统自动完成。给定一个查询字符串 s和一组所有可能的查询字符串,返回所有字符串集s作为前缀。 例如,给定的查询字符串 de和字符串的集合dog, deer, deal),返回deer, deal]. 提示:试试预处理字典到一个更高效的数据结构加快查询速度。

12.[Hard]There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. Given N, write a function that returns the number of unique ways you can climb the staircase. The order of the steps matters.
存在一个与N步楼梯,你可以爬上1或2的步骤。给定的N,编写一个函数,它返回的独特的方式你可以爬楼梯。步骤的顺序很重要。 For example, if N is 4, then there are 5 unique ways: 例如,如果N是4,然后有5个独特的方式:
1, 1, 1, 1
2, 1, 1
1, 2, 1
1, 1, 2
2, 2
What if, instead of being able to climb 1 or 2 steps at a time, you could climb any number from a set of positive integers X? For example, if X = {1, 3, 5}, you could climb 1, 3, or 5 steps at a time.
如果不是能够爬一次1或2的步骤,你可以爬上任何数量从一组X正整数?例如,如果X ={1, 3, 5},你可以爬1,3或5步骤一次。

13.[Hard]Given an integer k and a string s, find the length of the longest substring that contains at most k distinct characters.
For example, given s = “abcba” and k = 2, the longest substring with k distinct characters is “bcb”.
鉴于k一个整数和一个字符串年代,找到包含最多的最长的子字符串的长度k截然不同的角色。 例如,给定s =“abcba”和k = 2,最长的子串与k不同的字符是“bcb”。

14.[Medium] The area of a circle is defined as πr^2. Estimate π to 3 decimal places using a Monte Carlo method.
Hint: The basic equation of a circle is x2 + y2 = r2.
提示:一个圆的基本方程是x2 + y2 = r2。

15.[Medium]Given a stream of elements too large to store in memory, pick a random element from the stream with uniform probability.
给定一个流太大的元素存储在内存中,选择一个随机元素从流均匀概率。

16.[Easy] You run an e-commerce website and want to record the last N order ids in a log. Implement a data structure to accomplish this, with the following API:
record(order_id): adds the order_id to the log
get_last(i): gets the ith last element from the log. i is guaranteed to be smaller than or equal to N.
You should be as efficient with time and space as possible.
你运行一个电子商务的网站,想记录最后一次 N orderids日志。实现一个数据结构来实现这个目标,用以下API: 记录(order_id):将order_id添加到日志中 get_last(我):去年的第i个元素从日志。我保证是小于或等于N。 你应该尽可能与时间和空间效率。

17.[Hard]Suppose we represent our file system by a string in the following manner:
假设我们代表文件系统由一个字符串以下列方式:
The string “dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext” represents(代表):
dir
subdir1
subdir2
file.ext
The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.
的目录 dir包含一个空的子目录 subdir1和子目录 subdir2包含一个文件 file.ext.
The string “dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext” represents:
dir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.ext
The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext.
We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is “dir/subdir2/subsubdir2/file2.ext”, and its length is 32 (not including the double quotes).
Given a string representing the file system in the above format, return the length of the longest absolute path to a file in the abstracted file system. If there is no file in the system, return 0.
Note:
The name of a file contains at least a period and an extension.
The name of a directory or sub-directory will not contain a period.
的目录 dir包含两个子目录 subdir1和 subdir2. subdir1包含一个文件 file1.ext和一个空的二级子目录 subsubdir1. subdir2包含一个二级子目录 subsubdir2包含一个文件 file2.ext. 我们感兴趣的是找到最长(字符数)绝对路径文件在我们的文件系统。例如,在上面的第二个例子中,最长的绝对路径 "dir/subdir2/subsubdir2/file2.ext",其长度是32(不包括双引号)。 给定一个字符串代表文件系统在上面的格式,返回的长度最长的绝对路径的文件的文件系统。如果没有文件系统中,返回0。 注意: 一个文件的名称包含至少一段和一个扩展。 目录或子目录的名称不能包含一个时期。

18.[Hard]Given an array of integers and a number k, where 1 <= k <= length of the array, compute the maximum values of each subarray of length k.
For example, given array = [10, 5, 2, 7, 8, 7] and k = 3, we should get: [10, 7, 8, 8], since:
给定的整数数组和一个数字k,在1 k < < = =数组的长度,计算每个子数组长度的最大值k。 例如,给定数组=[10、5、2、7、8、7]和k = 3,我们应该:[10、7、8、8],因为: 10 = max(10, 5, 2) 7 = max(5, 2, 7) 8 = max(2, 7, 8) 8 = max(7, 8, 7) Do this in O(n) time and O(k) space. You can modify the input array in-place and you do not need to store the results. You can simply print them out as you compute them. 在O (n)时间和O (k)的空间。您可以修改输入数组就地和你不需要存储结果。你可以直接打印出来作为你计算它们。

19.[Medium] A builder is looking to build a row of N houses that can be of K different colors. He has a goal of minimizing cost while ensuring that no two neighboring houses are of the same color.
Given an N by K matrix where the nth row and kth column represents the cost to build the nth house with kth color, return the minimum cost which achieves this goal.
建筑工人正在寻求建立一个行N K的房屋,可以不同的颜色。他有一个目标,减少成本,同时确保没有两个邻近的房屋是相同的颜色。 给定一个N×K矩阵,第N行和K列代表了成本与K建立第N个房子的颜色,返回的最小成本达到这一目标。

20.[Easy]Given two singly linked lists that intersect at some point, find the intersecting node. The lists are non-cyclical.
For example, given A = 3 -> 7 -> 8 -> 10 and B = 99 -> 1 -> 8 -> 10, return the node with value 8.
In this example, assume nodes with the same value are the exact same node objects.
Do this in O(M + N) time (where M and N are the lengths of the lists) and constant space.
给定两个单链表相交在某种程度上,找到相交的节点。周期性的列表是不那么明显。 例如,给定一个= 3 - > 7 - 10 B = > 8 - > 99 - > 1 - > 8 - > 10,返回的节点值8。 在这个例子中,假设节点有相同的值相同的节点对象。 在O (M + N)时间(M和N是列表的长度)和恒定的空间。

21.[Easy]Given an array of time intervals (start, end) for classroom lectures (possibly overlapping), find the minimum number of rooms required.
For example, given [(30, 75), (0, 50), (60, 150)], you should return 2.
给定一组时间间隔(开始、结束)课堂讲座(可能是重叠的),找到所需的最小数量的房间。 例如,给定[(75)(0,50),(150)],您应该返回2。

22.[Medium]Given a dictionary of words and a string made up of those words (no spaces), return the original sentence in a list. If there is more than one possible reconstruction, return any of them. If there is no possible reconstruction, then return null.
For example, given the set of words ‘quick’, ‘brown’, ‘the’, ‘fox’, and the string “thequickbrownfox”, you should return [‘the’, ‘quick’, ‘brown’, ‘fox’].
Given the set of words ‘bed’, ‘bath’, ‘bedbath’, ‘and’, ‘beyond’, and the string “bedbathandbeyond”, return either [‘bed’, ‘bath’, ‘and’, 'beyond] or [‘bedbath’, ‘and’, ‘beyond’].
给定一个字典的单词和一个字符串组成的这些话(没有空格),返回列表中的原句。如果有多个可能的重建,其中任何一个。如果没有可能的重建,然后返回null。 例如,给定一组词,“快速”,“布朗”,“的”,“福克斯”,和字符串“thequickbrownfox”,你应该返回(“的”,“快”,“布朗”,“福克斯”)。 给定一组词,‘床’,‘洗澡’,‘bedbath’,’和‘,‘超越’,和字符串“bedbathandbeyond”,返回(“床”,“洗澡”,“和”,“除了]或[‘bedbath’,’和‘,‘超越’]。

23.[Easy]You are given an M by N matrix consisting of booleans that represents a board. Each True boolean represents a wall. Each False boolean represents a tile you can walk on.
Given this matrix, a start coordinate, and an end coordinate, return the minimum number of steps required to reach the end coordinate from the start. If there is no possible path, then return null. You can move up, left, down, and right. You cannot move through walls. You cannot wrap around the edges of the board.
给你一个M×N矩阵组成的布尔值,表示一个董事会。每个真正的布尔代表一堵墙。每个错误的布尔代表一个瓷砖可以行走。 鉴于这个矩阵,一个开始协调,和结束坐标,返回所需的最小数量的步骤达到协调从一开始就结束。如果没有可能的路径,然后返回null。你可以移动,左,下来,对吧。你不能穿过墙壁。你不能绕板的边缘。
For example, given the following board:
[[f, f, f, f],
[t, t, f, t],
[f, f, f, f],
[f, f, f, f]]
and start = (3, 0) (bottom left) and end = (0, 0) (top left), the minimum number of steps required to reach the end is 7, since we would need to go through (1, 2) because there is a wall everywhere else on the second row.
并开始= (3, 0)(左下)和结束= (0, 0)(左上),达到所需的最小数量的步骤是7,因为我们需要通过 (1, 2)因为其他地方有一堵墙在第二行。

24.[Medium]Implement locking in a binary tree. A binary tree node can be locked or unlocked only if all of its descendants or ancestors are not locked.
Design a binary tree node class with the following methods:
is_locked, which returns whether the node is locked
lock, which attempts to lock the node. If it cannot be locked, then it should return false. Otherwise, it should lock it and return true.
unlock, which unlocks the node. If it cannot be unlocked, then it should return false. Otherwise, it should unlock it and return true.
You may augment the node to add parent pointers or any other property you would like. You may assume the class is used in a single-threaded program, so there is no need for actual locks or mutexes. Each method should run in O(h), where h is the height of the tree.
在二叉树实现锁定。二叉树节点只能被锁定或解锁,如果所有的后裔或祖先没有锁。 设计一个二叉树节点类用以下方法: is_locked,它返回的节点是否被锁定了 lock的节点,它试图锁。如果它不能被锁定,那么它应该返回false。否则,它应该锁,返回true。 unlock,打开节点。如果它不能被解锁,那么它应该返回false。否则,它应该解锁,返回true。 你可能增加节点添加父指针或任何其他财产。你可以假设类是单线程程序中使用,所以没有实际锁或互斥的必要性。每个方法应该运行在O (h), h是树的高度。

25.[Hard]Implement regular expression matching with the following special characters:
. (period) which matches any single character
(asterisk) which matches zero or more of the preceding element
That is, implement a function that takes in a string and a valid regular expression and returns whether or not the string matches the regular expression.
For example, given the regular expression “ra.” and the string “ray”, your function should return true. The same regular expression on the string “raymond” should return false.
Given the regular expression “.*at” and the string “chat”, your function should return true. The same regular expression on the string “chats” should return false.
实现正则表达式匹配以下特殊字符: .(时期)匹配任何单个的字符 *(星号)匹配零个或多个前元素 ,实现一个函数,它接受一个字符串,并返回一个有效的正则表达式的字符串是否匹配的正则表达式。 例如,给定正则表达式“类风湿性关节炎”。和字符串“雷”,你的函数应该返回true。同一正则表达式字符串“雷蒙德”应该返回false。 给定的正则表达式”。*在弦”和“聊天”,你的函数应该返回true。同一正则表达式字符串“聊天”应该返回false。

26.[Medium]Given a singly linked list and an integer k, remove the kth last element from the list. k is guaranteed to be smaller than the length of the list.
The list is very long, so making more than one pass is prohibitively expensive.
Do this in constant space and in one pass.
给定一个单链表和一个整数k, k从列表中最后一个元素。k是保证小于列表的长度。 列表很长,使得多个传递是非常昂贵的。 在恒定的空间,在一个通过

27.[Easy]Given a string of round, curly, and square open and closing brackets, return whether the brackets are balanced (well-formed).
For example, given the string “([])”, you should return true.
Given the string “([)]” or “((()”, you should return false.
鉴于一系列圆,花,广场开放和关闭括号,括号是否返回平衡(良好的)。 例如,给定的字符串”([])[]({})”,你应该返回true。 给定的字符串”(())”或“((()”,你应该返回false。

28.[Medium]Write an algorithm to justify text. Given a sequence of words and an integer line length k, return a list of strings which represents each line, fully justified.
写一个算法来证明文本。给出一个单词和一个整数序列k线长,返回一个字符串列表,每一行代表,完全有道理的。
More specifically, you should have as many words as possible in each line. There should be at least one space between each word. Pad extra spaces when necessary so that each line has exactly length k. Spaces should be distributed as equally as possible, with the extra spaces, if any, distributed starting from the left.
更具体地说,你应该尽可能多的单词在每一行。至少应该有一个每个单词之间的空间。垫在必要时额外的空间,使每一行完全k。空间长度应尽可能同样分布,与额外的空间,如果有的话,分布从左边开始
If you can only fit one word on a line, then you should pad the right-hand side with spaces.
如果你只能适应一个词在一条线,那么你应该垫右侧与空间
Each word is guaranteed not to be longer than k.
保证每个单词不超过k
For example, given the list of words [“the”, “quick”, “brown”, “fox”, “jumps”, “over”, “the”, “lazy”, “dog”] and k = 16, you should return the following:
例如,考虑到单词列表(“的”,“快”,“布朗”,“福克斯”,“跳”,“在”,“”,“懒”,“狗”)和k = 16,您应该返回以下:
[“the quick brown”, # 1 extra space on the left
“fox jumps over”, # 2 extra spaces distributed evenly
“the lazy dog”] # 4 extra spaces distributed evenly

29.[Easy]Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as a single count and character. For example, the string “AAAABBBCCDAA” would be encoded as “4A3B2C1D2A”.
Implement run-length encoding and decoding. You can assume the string to be encoded have no digits and consists solely of alphabetic characters. You can assume the string to be decoded is valid.
行程长度编码是一个快速和简单的编码字符串的方法。基本思想是代表作为一个计数和连续重复字符。例如,字符串“AAAABBBCCDAA”将被编码为“4 a3b2c1d2a”。 实现行程长度编码和解码。你可以假设字符串编码没有数字,仅由字母字符。你可以假设字符串解码是有效的。

30.[Medium] You are given an array of non-negative integers that represents a two-dimensional elevation map where each element is unit-width wall and the integer is the height. Suppose it will rain and all spots between two walls get filled up.
Compute how many units of water remain trapped on the map in O(N) time and O(1) space.
For example, given the input [2, 1, 2], we can hold 1 unit of water in the middle.
Given the input [3, 0, 1, 3, 0, 5], we can hold 3 units in the first index, 2 in the second, and 3 in the fourth index (we cannot hold 5 since it would run off to the left), so we can trap 8 units of water.
你给出一个非负整数数组,每个元素代表一个二维高程地图是宽墙和整数的高度。假设会下雨和两堵墙之间的所有点填满。 计算有多少单位的水在地图上被困在O (N)时间和O(1)的空间。 例如,给定输入(2,1,2),我们可以举行一个单位的水在中间。 给定的输入(3 0 1 3 0 5],我们在第一个索引可以容纳3单位,2在第二,第四指数和3(我们不能持有5因为它会跑到左边),所以我们可以陷阱8单元的水。

31.[Easy]The edit distance between two strings refers to the minimum number of character insertions, deletions, and substitutions required to change one string to the other. For example, the edit distance between “kitten” and “sitting” is three: substitute the “k” for “s”, substitute the “e” for “i”, and append a “g”.
Given two strings, compute the edit distance between them.
两个字符串之间的编辑距离指的是最小数量的字符插入,删除和替换需要改变一个字符串。例如,之间的编辑距离“小猫”和“坐”是三:替代“s”的“k”,用“我”的“e”,并附加“g”。 鉴于两个字符串,计算它们之间的编辑距离。

32.[Hard]Suppose you are given a table of currency exchange rates, represented as a 2D array. Determine whether there is a possible arbitrage: that is, whether there is some sequence of trades you can make, starting with some amount A of any currency, so that you can end up with some amount greater than A of that currency.
There are no transaction costs and you can trade fractional quantities.
假设你是给定一个货币汇率表,表示为一个二维数组。判断是否有可能的套利:那就是,是否有你可以做一系列的交易,从一定数量的货币,这样你就可以得到一些数量大于货币。 没有交易成本,你可以交易部分数量。

33.[Easy] Compute the running median of a sequence of numbers. That is, given a stream of numbers, print out the median of the list so far on each new element.
计算的运行中一个数字序列。给定一个数字流,打印出的中值到目前为止在每个新元素列表。
Recall that the median of an even-numbered list is the average of the two middle numbers.
回想一下,一个偶数的中值列表中间的两个数的平均值。
For example, given the sequence [2, 1, 5, 7, 2, 0, 5], your algorithm should print out:
例如,考虑到序列(2、1、5、7、2 0 5],你的算法应该打印出:
2
1.5
2
3.5
2
2
2

34.[Medium]Given a string, find the palindrome that can be made by inserting the fewest number of characters as possible anywhere in the word. If there is more than one palindrome of minimum length that can be made, return the lexicographically earliest one (the first one alphabetically).
For example, given the string “race”, you should return “ecarace”, since we can add three letters to it (which is the smallest amount to make a palindrome). There are seven other palindromes that can be made from “race” by adding three letters, but “ecarace” comes first alphabetically.
As another example, given the string “google”, you should return “elgoogle”.
给定一个字符串,找到可以通过插入的回文的最少字符数尽可能在这个词。如果有不止一个回文的最小长度,返回按最早的一个(第一个字母顺序排列)。 例如,给定的字符串“种族”,你应该返回“ecarace”,因为我们可以添加三个字母(这是最小的数量做一个回文)。有七个回文,可以通过添加三个字母由“种族”,但“ecarace”是第一个字母顺序排列。 另外一个例子,给定的字符串“谷歌”,你应该返回“elgoogle”。

35.[Hard] Given an array of strictly the characters ‘R’, ‘G’, and ‘B’, segregate the values of the array so that all the Rs come first, the Gs come second, and the Bs come last. You can only swap elements of the array.
Do this in linear time and in-place.
For example, given the array [‘G’, ‘B’, ‘R’, ‘R’, ‘B’, ‘R’, ‘G’], it should become [‘R’, ‘R’, ‘R’, ‘G’, ‘G’, ‘B’, ‘B’].
给定一组严格的字符' R ', ' G ',和' B ',隔离的值数组,这样所有的Rs来首先,Gs是第二,和Bs。你只能交换数组的元素。 这样做在线性时间和就地。 例如,考虑到array [' G ', ' B ', ' R ', ' R ', ' B ', ' R ', ' G '],它应该成为[‘R’,‘R’,‘R’,‘G’,‘G’, ' B ', ' B ']。

36.[Medium] Given the root to a binary search tree, find the second largest node in the tree.
给定一个二叉搜索树的根,找到第二大节点树。

37.[Easy]The power set of a set is the set of all its subsets. Write a function that, given a set, generates its power set.
For example, given the set {1, 2, 3}, it should return {{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}.
You may also use a list or array to represent a set.
权力的一组是一组集的子集。写一个函数,给定一组生成它的力量。 例如,给定一组 {1, 2, 3},它应该返回 {{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}. 你也可以使用列表或数组来表示一组。

38.[Medium] Conway’s Game of Life takes place on an infinite two-dimensional board of square cells. Each cell is either dead or alive, and at each tick, the following rules apply:
康威的生活游戏发生在无限平方细胞的二维板。每个细胞要么是死是活,在每个蜱虫,以下规则适用:
Any live cell with less than two live neighbours dies.
Any live cell with two or three live neighbours remains living.
Any live cell with more than three live neighbours dies.
Any dead cell with exactly three live neighbours becomes a live cell.
A cell neighbours another cell if it is horizontally, vertically, or diagonally adjacent.
任何活细胞不到两住的邻居死了。 任何活细胞与两个或三个住邻国仍然生活。 任何活细胞中有超过三个活的邻居死了。 任何死细胞完全三活邻国变成一个活细胞。
Implement Conway’s Game of Life. It should be able to be initialized with a starting list of live cell
细胞邻居另一个细胞是否水平,垂直,或对角相邻。
coordinates and the number of steps it should run for. Once initialized, it should print out the board state at each step. Since it’s an infinite board, print out only the relevant coordinates, i.e. from the top-leftmost live cell to bottom-rightmost live cell.
实现康威的生活游戏。它应该能够与活细胞的启动列表初始化坐标和步骤的数量应该竞选。一旦初始化,它每一步应打印出板状态。因为它是一个无限,只打印出相关的坐标,即从top-leftmost活细胞bottom-rightmost活细胞
You can represent a live cell with an asterisk () and a dead cell with a dot (.).
你可以用星号(代表一个活细胞
)和死细胞与一个点(.).

39.[Hard]Given an array of integers where every integer occurs three times except for one integer, which only occurs once, find and return the non-duplicated integer.
For example, given [6, 1, 3, 3, 3, 6, 6], return 1. Given [13, 19, 13, 13], return 19.
Do this in O(N) time and O(1) space.
给定的整数数组,每个整数发生三次除了一个整数,它只发生一次,找到并返回压缩成整数。 例如,给定[6,1,3,3,3,6,6],返回1。鉴于[13]13日,19日,13日,19日返回。 在O (N)时间和O(1)的空间。

40.[Medium]Given an unordered list of flights taken by someone, each represented as (origin, destination) pairs, and a starting airport, compute the person’s itinerary. If no such itinerary exists, return null. If there are multiple possible itineraries, return the lexicographically smallest one. All flights must be used in the itinerary.
给定一个无序列表的航班被某人,每个表示为(来源、目的地)对,和一个机场,开始计算人的行程。如果不存在这样的行程,返回null。如果有多个可能的行程,返回按最小的一个。必须使用所有航班的行程。
For example, given the list of flights [(‘SFO’, ‘HKO’), (‘YYZ’, ‘SFO’), (‘YUL’, ‘YYZ’), (‘HKO’, ‘ORD’)] and starting airport ‘YUL’, you should return the list [‘YUL’, ‘YYZ’, ‘SFO’, ‘HKO’, ‘ORD’].
例如,给定的航班列表[(“旧金山”、“女童教育活动”)(“机会”,“旧金山”)(“尤尔•”、“机会”)(“女童教育活动”,“奥德”)]和机场“尤尔•”开始,你应该返回列表(“尤尔•”、“机会”,“旧金山”,“女童教育活动”,“奥德”)。
Given the list of flights [(‘SFO’, ‘COM’), (‘COM’, ‘YYZ’)] and starting airport ‘COM’, you should return null.
考虑到航班列表((“旧金山”、“COM”), (“COM”,“机会”)]和机场“COM”开始,你应该返回null。
Given the list of flights [(‘A’, ‘B’), (‘A’, ‘C’), (‘B’, ‘C’), (‘C’, ‘A’)] and starting airport ‘A’, you should return the list [‘A’, ‘B’, ‘C’, ‘A’, ‘C’] even though [‘A’, ‘C’, ‘A’, ‘B’, ‘C’] is also a valid itinerary. However, the first one is lexicographically smaller.
给定的航班列表((“A”、“B”)(“一个”、“C”), (“B”、“C”) (“C”,“A”)]和机场“A”开始,你应该返回列表(A, B, C,“‘' C ']尽管[' A ', ' C ', ' ', ' B ', ' C ']也是一个有效的行程。然而,第一个是按小。

41.[Hard] Given a list of integers S and a target number k, write a function that returns a subset of S that adds up to k. If such a subset cannot be made, then return null.
Integers can appear more than once in the list. You may assume all numbers in the list are positive.
For example, given S = [12, 1, 61, 5, 9, 2] and k = 24, return [12, 9, 2, 1] since it sums up to 24.
给定一个整数列表和目标数k,编写一个函数,它返回一个子集S, k。如果这样不能使一个子集,然后返回null。 整数可以出现不止一次在列表中。你可以假设列表中的所有数字都是正的。 例如,鉴于S =[12, 61、5、9日2]和k = 24日返回[2 12日,9日,1]因为它总结了24。

42.[Easy]Implement a stack that has the following methods:
push(val), which pushes an element onto the stack
pop(), which pops off and returns the topmost element of the stack. If there are no elements in the stack, then it should throw an error or return null.
max(), which returns the maximum value in the stack currently. If there are no elements in the stack, then it should throw an error or return null.
Each method should run in constant time.
实现一个堆栈,有以下方法: 推动(val),它把一个元素压入堆栈 pop(),会弹出并返回最顶层元素的堆栈。如果没有栈中的元素,那么它应该抛出一个错误或返回null。 max(),它返回当前栈的最大价值。如果没有栈中的元素,那么它应该抛出一个错误或返回null。 每个方法应该在常数时间运行

43.[Medium] We can determine how “out of order” an array A is by counting the number of inversions it has. Two elements A[i] and A[j] form an inversion if A[i] > A[j] but i < j. That is, a smaller element appears after a larger element.
我们可以确定“秩序”是通过计算数组逆序对它的数量。两个元素 A[i]和 A[j]形成一个反转,如果 A[i] > A[j]但 i < j。也就是说,一个较小的元素出现在一个更大的元素。
Given an array, count the number of inversions it has. Do this faster than O(N^2) time.
给定一个数组,计算反演的数量。这样做比O (N ^ 2)时间。
You may assume each element in the array is distinct.
你可能认为数组中的每个元素是截然不同的。
For example, a sorted list has zero inversions. The array [2, 4, 1, 3, 5] has three inversions: (2, 1), (4, 1), and (4, 3). The array [5, 4, 3, 2, 1] has ten inversions: every distinct pair forms an inversion.
例如,一个排序列表反演为零。数组(2、4、1、3、5)有三个倒置:(2,1),(4,1)和数组(4,3)。[5、4、3、2、1)有十个反转:每一个不同的形式倒置。

44.[Easy] This problem was asked by Two Sigma.
Using a function rand5() that returns an integer from 1 to 5 (inclusive) with uniform probability, implement a function rand7() that returns an integer from 1 to 7 (inclusive).
这个问题问两个σ。 使用一个函数 rand5()从1到5,它返回一个整数(包容)有统一的概率,实现一个函数 rand7()从1到7,它返回一个整数(包容)。

45.[Hard]Given a string, find the longest palindromic contiguous substring. If there are more than one with the maximum length, return any one.
For example, the longest palindromic substring of “aabcdcb” is “bcdcb”. The longest palindromic substring of “bananas” is “anana”.
给定一个字符串,查找最长回文连续的子字符串。如果有一个以上的最大长度,返回任何一个。 例如,最长的回文字符串“aabcdcb”是“bcdcb”。最长的回文字符串“香蕉”是“香蕉”。

46.[Easy] Given a array of numbers representing the stock prices of a company in chronological order, write a function that calculates the maximum profit you could have made from buying and selling that stock once. You must buy before you can sell it.
给定一组数字代表一个公司的股票价格按时间顺序,写一个函数,计算出最大的利润可以由买卖股票。你必须购买才能把它卖掉。 For example, given [9, 11, 8, 5, 7, 10], you should return 5, since you could buy the stock at 5 dollars and sell it at 10 dollars. 例如,给定[8,9,11日5、7、10],您应该返回5,因为你可以买股票在5美元,出售它在10美元。

47.[Medium] Given pre-order and in-order traversals of a binary tree, write a function to reconstruct the tree.
鉴于预购和二叉树的中序遍历,编写一个函数来重建树。
For example, given the following preorder traversal:
例如,给出以下前序遍历:
[a, b, d, e, c, f, g]
And the following inorder traversal:
和下面的有条不紊地进行遍历:
[d, b, e, a, f, c, g]
You should return the following tree:
你应该返回下面的树:
a
/
b c
/ \ /
d e f g

48.[Medium] Given an array of numbers, find the maximum sum of any contiguous subarray of the array.
给定一组数据,找到最大的和连续的子数组的数组
For example, given the array [34, -50, 42, 14, -5, 86], the maximum sum would be 137, since we would take elements 42, 14, -5, and 86.
例如,考虑到array[5, 34岁,-50年,42岁,14日86],最大金额是137,因为我们将元素42岁,14日5、86。
Given the array [-5, -1, -8, -9], the maximum sum would be 0, since we would not take any elements.
给定数组(5、1、8、9)的最大金额是0,因为我们不会采取任何元素。
Do this in O(N) time. 在O (N)时间。

49.[Easy]Suppose an arithmetic expression is given as a binary tree. Each leaf is an integer and each internal node is one of ‘+’, ‘−’, ‘∗’, or ‘/’.
假设一个算术表达式作为一棵二叉树。每片叶子是一个整数,每个内部节点是‘+’,‘−’,‘∗’,或‘/’。
Given the root to such a tree, write a function to evaluate it.
有根树,编写一个函数来评估它。
For example, given the following tree:

/
+ +
/ \ /
3 2 4 5
You should return 45, as it is (3 + 2) * (4 + 5). 你应该返回45,这是(3 + 2)* (4 + 5)。

50.[Hard] Implement an LRU (Least Recently Used) cache. It should be able to be initialized with a cache size n, and contain the following methods:
set(key, value): sets key to value. If there are already n items in the cache and we are adding a new item, then it should also remove the least recently used item.
get(key): gets the value at key. If no such key exists, return null.
Each operation should run in O(1) time.
实现一个LRU(最近最少使用)缓存。它应该能被初始化缓存大小 n,包含以下方法: set(key, value):集 key来 value。如果已经有 n物品在缓存中,我们添加一个新条目,那么它也应该删除最近最少使用的项目。 get(key):获取价值 key。如果没有这样的键存在,返回null。 每个操作应该运行在O(1)时间。

51.[Medium] Implement a queue using two stacks. Recall that a queue is a FIFO (first-in, first-out) data structure with the following methods: enqueue, which inserts an element into the queue, and dequeue, which removes it.
用两个栈实现一个队列。回想一下,一个队列是一个FIFO(先进先出)数据结构用以下方法: enqueue,在队列中插入一个元素 dequeue,删除它。

52.[Hard] Sudoku is a puzzle where you’re given a partially-filled 9 by 9 grid with digits. The objective is to fill the grid with the constraint that every row, column, and box (3 by 3 subgrid) must contain all of the digits from 1 to 9.
Implement an efficient sudoku solver.
数独是一个谜,你有未填满9的9网格数字。约束的目的是填补网格,每一行,列,盒子(3×3次网格)必须包含所有的数字从1到9。 实现一个高效的数独。

53.[Easy]Implement a URL shortener with the following methods:
shorten(url), which shortens the url into a six-character alphanumeric string, such as zLg6wl.
restore(short), which expands the shortened string into the original url. If no such shortened string exists, return null.
Hint: What if we enter the same URL twice?
实现一个网址用以下方法: shorten(url),缩短url为六字字母数字字符串,如 zLg6wl. restore(short),扩展了缩短到原始url的字符串。如果没有这样的短字符串存在,返回null。 提示:如果我们两次输入相同的URL ?

54.[Medium] Given an undirected graph represented as an adjacency matrix and an integer k, write a function to determine whether each vertex in the graph can be colored such that no two adjacent vertices share the same color using at most k colors.

给定一个无向图表示为邻接矩阵和一个整数k,编写一个函数来确定图中每个顶点可以彩色,没有两个相邻顶点共享相同的颜色最多使用k的颜色。

55.[Medium] Given a string s and an integer k, break up the string into multiple lines such that each line has a length of k or less. You must break it up so that words don’t break across lines. Each line has to have the maximum possible amount of words. If there’s no way to break the text up, then return null.
给定一个字符串和整数k,将字符串分成多行,每一行的长度k或更少。你必须打破这词不打破跨线。每一行有最大可能的单词量。如果没有办法打破文本,然后返回null。
You can assume that there are no spaces at the ends of the string and that there is exactly one space between each word.
你可以假设没有空格的字符串,每个单词之间有一个空间。
For example, given the string “the quick brown fox jumps over the lazy dog” and k = 10, you should return: [“the quick”, “brown fox”, “jumps over”, “the lazy”, “dog”]. No string in the list has a length of more than 10.
例如,给定的字符串“只敏捷的棕色狐狸跳过了懒惰的狗”和k = 10,你应该返回:[“快速”、“棕色狐狸”,“跳过”,“懒”,“狗”)。没有列表中的字符串长度超过10。

56.[Medium] An sorted array of integers was rotated an unknown number of times.
Given such an array, find the index of the element in the array in faster than linear time. If the element doesn’t exist in the array, return null.
For example, given the array [13, 18, 25, 2, 8, 10] and the element 8, return 4 (the index of 8 in the array).
You can assume all the integers in the array are unique.
一个排序的整数数组旋转一个未知的次数。 给这样的一个数组,数组中找到元素的索引的速度比线性时间。如果数组中的元素不存在,返回null。 例如,考虑到array[13日,18日,25日2 8 10]8和元素,返回4(索引数组中8)。 你可以假设所有的整数数组中是独一无二的。

57.[Hard] Implement a file syncing algorithm for two computers over a low-bandwidth network. What if we know the files in the two computers are mostly the same?
实现两台电脑的文件同步算法在低带宽的网络。如果我们知道两台电脑中的文件大多是一样的吗?

58.[Medium]Given a multiset of integers, return whether it can be partitioned into two subsets whose sums are the same.
给定一个多重集的整数,返回是否可以分割成两个子集的金额都是一样的。
For example, given the multiset {15, 5, 20, 10, 35, 15, 10}, it would return true, since we can split it up into {15, 5, 10, 15, 10} and {20, 35}, which both add up to 55.
例如,考虑到多重集 {15, 5, 20, 10, 35, 15, 10},它将返回true,因为我们可以把它分成 {15, 5, 10, 15, 10}和 {20, 35},都说明了什么 55. Given the multiset {15, 5, 20, 10, 35}, it would return false, since we can't split it up into two subsets that add up to the same sum. 考虑到多重集 {15, 5, 20, 10, 35},它将返回false,因为我们不能把它分成两个子集,加起来总和相同。

59.[Medium]Implement integer exponentiation. That is, implement the pow(x, y) function, where x and y are integers and returns x^y.
Do this faster than the naive method of repeated multiplication.
For example, pow(2, 10) should return 1024.
实现整数求幂。也就是说,实现 pow(x, y)函数, x和 y是整数,并返回 x^y. 这样做比天真的方法重复的乘法。 例如, pow(2, 10)应该返回1024。

60.[Medium]There is an N by M matrix of zeroes. Given N and M, write a function to count the number of ways of starting at the top-left corner and getting to the bottom-right corner. You can only move right or down.
有一个N×M矩阵的零。鉴于N和M,编写一个函数来计算的方法的数量从左上角和右下角。你只能向右走。
For example, given a 2 by 2 matrix, you should return 2, since there are two ways to get to the bottom-right:
例如,给定一个2×2的矩阵,你应该返回2,因为右下方有两种方法:
Right, then down
Down, then right
Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right.
向右,然后再向下
下,然后右
给定一个5 * 5矩阵,有70种方法去右下角。
61.[Easy]Given a 2D matrix of characters and a target word, write a function that returns whether the word can be found in the matrix by going left-to-right, or up-to-down.
给定一个二维矩阵的字符和一个目标词,写一个函数,它返回这个词是否可以在矩阵通过从左到右,或建构。 例如,给定的矩阵如下:
For example, given the following matrix:
[[‘F’, ‘A’, ‘C’, ‘I’],
[‘O’, ‘B’, ‘Q’, ‘P’],
[‘A’, ‘N’, ‘O’, ‘B’],
[‘M’, ‘A’, ‘S’, ‘S’]]
and the target word ‘FOAM’, you should return true, since it’s the leftmost column. Similarly, given the target word ‘MASS’, you should return true, since it’s the last row.
和目标词“泡沫”,你应该返回true,因为它是最左边的列。同样,考虑到目标词“质量”,你应该返回true,因为这是最后一行

62.[Hard]A knight’s tour is a sequence of moves by a knight on a chessboard such that all squares are visited once.
Given N, write a function to return the number of knight’s tours on an N by N chessboard.
骑士之旅是一个序列的举动,棋盘上的骑士,这样所有方块都访问一次。 给定的N,编写一个函数来返回骑士的数量对N * N棋盘的旅游。

63.[Easy]Given a N by M matrix of numbers, print out the matrix in a clockwise spiral.
For example, given the following matrix:
给定一个N×M矩阵的数字,打印出顺时针螺旋矩阵。 例如,给定的矩阵如下:
[[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]]
You should print out the following: 你应该打印出以下几点:
1
2
3
4
5
10
15
20
19
18
17
16
11
6
7
8
9
14
13
12

64.[Medium]Assume you have access to a function toss_biased() which returns 0 or 1 with a probability that’s not 50-50 (but also not 0-100 or 100-0). You do not know the bias of the coin.
Write a function to simulate an unbiased coin toss.
假设您有一个函数 toss_biased()它返回0或1的概率不是50 - 50(但也不是0 - 100或100 - 0)。你不知道硬币的偏差。 写一个函数来模拟一个无偏抛硬币。

65.[Hard]Implement an LFU (Least Frequently Used) cache. It should be able to be initialized with a cache size n, and contain the following methods:
set(key, value): sets key to value. If there are already n items in the cache and we are adding a new item, then it should also remove the least frequently used item. If there is a tie, then the least recently used key should be removed.
get(key): gets the value at key. If no such key exists, return null.
Each operation should run in O(1) time.
实现一个LFU(最常用)缓存。它应该能被初始化缓存大小 n,包含以下方法: set(key, value):集 key来 value。如果已经有 n物品在缓存中,我们添加一个新条目,那么它也应该删除最经常使用的物品。如果有一个领带,然后最近最少使用的键应该被删除。 get(key):获取价值 key。如果没有这样的键存在,返回null。 每个操作应该运行在O(1)时间。

66.[Medium]On our special chessboard, two bishops attack each other if they share the same diagonal. This includes bishops that have another bishop located between them, i.e. bishops can attack through pieces.
在我们特殊的棋盘,两个主教攻击如果它们共享相同的对角线。这包括主教,有另一个主教位于它们之间,即主教可以通过碎片攻击。
You are given N bishops, represented as (row, column) tuples on a M by M chessboard. Write a function to count the number of pairs of bishops that attack each other. The ordering of the pair doesn’t matter: (1, 2) is considered the same as (2, 1).
你给出N主教,表示为(行、列)元组在M×M棋盘。编写一个函数来计算对主教的数量,互相攻击。对不重要的顺序:(1、2)被认为是一样的(2,1)。
For example, given M = 5 and the list of bishops:
例如,鉴于M = 5和主教的列表:
(0, 0)
(1, 2)
(2, 2)
(4, 0)
The board would look like this:
[b 0 0 0 0]
[0 0 b 0 0]
[0 0 b 0 0]
[0 0 0 0 0]
[b 0 0 0 0]
You should return 2, since bishops 1 and 3 attack each other, as well as bishops 3 and 4.
你应该返回2,因为主教1和3的攻击对方,以及主教3和4。

67.[Easy]Given a list of integers, return the largest product that can be made by multiplying any three integers.
For example, if the list is [-10, -10, 5, 2], we should return 500, since that’s -10 * -10 * 5.
You can assume the list has at least three integers.
给定一个整数列表,返回最大的产品,可以由任意三个整数相乘。 例如,如果是列表 [-10, -10, 5, 2],我们应该回报 500,因为这是 -10 * -10 * 5. 你可以假设至少有三个整数列表。

68.[Easy]A number is considered perfect if its digits sum up to exactly 10.
Given a positive integer n, return the n-th perfect number.
For example, given 1, you should return 19. Given 2, you should return 28.
许多被认为是完美的如果它的确切数字总结10。 给出一个正整数 n,返回 nth完美的数字。 例如,1,您应该返回19。2,你应该返回28。

69.[Easy] Using a function rand7() that returns an integer from 1 to 7 (inclusive) with uniform probability, implement a function rand5() that returns an integer from 1 to 5 (inclusive).
使用一个函数 rand7()从1到7,它返回一个整数(包容)有统一的概率,实现一个函数 rand5()从1到5,它返回一个整数(包容)。

70.[Hard] In a directed graph, each node is assigned an uppercase letter. We define a path’s value as the number of most frequently-occurring letter along that path. For example, if a path in the graph goes through “ABACA”, the value of the path is 3, since there are 3 occurrences of ‘A’ on the path.
在有向图中,每个节点分配一个大写字母。我们定义了一个路径的值作为最常见的信沿着这条道路的数量。例如,如果一个路径图中经过“马尼拉麻”,路径的值是3,因为有3出现的“a”的道路上。
Given a graph with n nodes and m directed edges, return the largest value path of the graph. If the largest value is infinite, then return null.
给定一个图, n节点和 m定向边缘,返回路径图的最大价值。如果最大的价值是无限的,那么返回null。
The graph is represented with a string and an edge list. The i-th character represents the uppercase letter of the i-th node. Each tuple in the edge list (i, j) means there is a directed edge from the i-th node to the j-th node. Self-edges are possible, as well as multi-edges.
图为代表一个字符串和一个边列表。的 ith字符代表的大写字母 ith节点。每个元组的列表 (i, j)意味着有一个导演的边缘 ith节点的 jth节点。Self-edges是可能的,以及multi-edges。 例如,下面的输入图:
For example, the following input graph:
ABACA
[(0, 1),
(0, 2),
(2, 3),
(3, 4)]
Would have maximum value 3 using the path of vertices
[0, 2, 3, 4], (A, A, C, A).
The following input graph:
A
[(0, 0)]
Should return null, since we have an infinite loop.
应该返回null,因为我们有一个无限循环。

71.[Easy]Given the head of a singly linked list, reverse it in-place.
给定一个单链表,就地扭转它

72.[Medium] Suppose you have a multiplication table that is N by N. That is, a 2D array where the value at the i-th row and j-th column is (i + 1) * (j + 1) (if 0-indexed) or i * j (if 1-indexed).
假设您有一个乘法表是N×N, 2 d值的数组 ith行和 jth列 (i + 1) * (j + 1)(如果就是) i * j(如果1-indexed)。 Given integers N and X, write a function that returns the number of times X appears as a value in an N by N multiplication table. 给定的整数N和X,编写一个函数,它返回X作为一个值出现的次数N×N的乘法表。
For example, given N = 6 and X = 12, you should return 4, since the multiplication table looks like this:
例如,给定的N = 6和X = 12,你应该返回4,因为乘法表是这样的:
| 1 | 2 | 3 | 4 | 5 | 6 |
| 2 | 4 | 6 | 8 | 10 | 12 |
| 3 | 6 | 9 | 12 | 15 | 18 |
| 4 | 8 | 12 | 16 | 20 | 24 |
| 5 | 10 | 15 | 20 | 25 | 30 |
| 6 | 12 | 18 | 24 | 30 | 36 |
表中,有4个12的。
And there are 4 12’s in the table.

73.[Hard]Given an array of numbers, find the length of the longest increasing subsequence in the array. The subsequence does not necessarily have to be contiguous.
给定一组数据,找出最长上升子序列的长度的数组。子序列不一定必须是连续的
For example, given the array [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15], the longest increasing subsequence has length 6: it is 0, 2, 6, 9, 11, 15.
例如,考虑到array[2 4 0, 8日,12日,10日,6日,14日,1,9日,5日,13日,11日,7日,15),最长上升子序列的长度6:它是0,2、6、9、11、15。

74.[Medium] You are given an N by M 2D matrix of lowercase letters. Determine the minimum number of columns that can be removed to ensure that each row is ordered from top to bottom lexicographically. That is, the letter at each column is lexicographically later as you go down each row. It does not matter whether each row itself is ordered lexicographically.
给你一个N×M 2 d矩阵的小写字母。确定最小数量的列可以删除,以确保每一行命令从上到下按。,每一列的字母是按后来当你沿着每一行。不管每一行本身是有序的字母顺序进行。
For example, given the following table: 例如,得到下表:
cba
daf
ghi
This is not ordered because of the a in the center. We can remove the second column to make it ordered: 这不是命令,因为在中心。我们可以删除第二列命令:
ca
df
gi
So your function should return 1, since we only needed to remove 1 column.
As another example, given the following table:
所以你的函数应该返回1,因为我们只需要删除1列。
另外一个例子,得到下表:
abcdef
Your function should return 0, since the rows are already ordered (there’s only one row).
As another example, given the following table:
你的函数应该返回0,因为已经命令行(只有一行)。
另外一个例子,得到下表:
zyx
wvu
tsr
Your function should return 3, since we would need to remove all the columns to order it.
你的函数应该返回3,因为我们需要删除所有的列顺序。

75.[Easy] Given a list of possibly overlapping intervals, return a new list of intervals where all overlapping intervals have been merged.
The input list is not necessarily ordered in any way.
For example, given [(1, 3), (5, 8), (4, 10), (20, 25)], you should return [(1, 3), (4, 10), (20, 25)].
给定的列表可能重叠的时间间隔,间隔返回一个新列表,所有重叠区间合并。 输入以任何方式不一定是有序列表。 例如,给定[(1,3)(5、8)、(10),(20、25)],您应该返回[(1,3)(10)、(20、25)]

76.[Medium]Given k sorted singly linked lists, write a function to merge all the lists into one sorted singly linked list.
鉴于k有序单链表,写一个函数合并成一个有序单链表的所有列表

77.[Medium] Given an array of integers, write a function to determine whether the array could become non-decreasing by modifying at most 1 element.
For example, given the array [10, 5, 7], you should return true, since we can modify the 10 into a 1 to make the array non-decreasing.
Given the array [10, 5, 1], you should return false, since we can’t modify any one element to get a non-decreasing array.
给定一个整数数组,编写一个函数来确定数组可能成为引入通过修改最多1元素。 例如,考虑到数组(10、5、7),您应该返回true,因为我们可以修改数组引入10到1。 考虑到数组(10、5、1),您应该返回false,既然我们不能修改任何一个元素引入数组。

78.[Easy] Given the root of a binary tree, return a deepest node. For example, in the following tree, return d.
鉴于二叉树的根,返回一个最深的节点。例如,在下面的树,返回d。
a
/
b c
/
d

79.[Easy] Given a mapping of digits to letters (as in a phone number), and a digit string, return all possible letters the number could represent. You can assume each valid number in the mapping is a single digit.
For example if {“2”: [“a”, “b”, “c”], 3: [“d”, “e”, “f”], …} then “23” should return [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf"].
给定一个映射的数字字母(如电话号码),和一个数字的字符串,返回所有可能的字母代表数量。你可以假设每个有效数字的映射是一个数字。 例如如果{“2”(“a”、“b”、“c”), 3: [“d”、“e”、“f”],…}然后“23”应该返回(“广告”,“ae”、“对焦”,“bd”,“是”,“男朋友”、“cd”、“ce”、“cf”)。

80.[Easy] Using a read7() method that returns 7 characters from a file, implement readN(n) which reads n characters.
For example, given a file with the content “Hello world”, three read7() returns “Hello w”, “orld” and then “”.
使用read7()方法,它返回7字符从一个文件中,实现readN n (n),读取字符。 例如,给定一个文件的内容“Hello world”,三个read7()返回“你好w”,“上”,然后“。

83.[Medium] Invert a binary tree. 反转一棵二叉树
For example, given the following tree:
a
/
b c
/ \ /
d e f
should become:
a
/
c b
\ /
f e d

84.[Medium] Given a matrix of 1s and 0s, return the number of “islands” in the matrix. A 1 represents land and 0 represents water, so an island is a group of 1s that are neighboring whose perimeter is surrounded by water.
For example, this matrix has 4 islands.
给定一个矩阵的0和1,返回“岛屿”矩阵的数量。1代表土地,0代表水,所以一个岛屿是一组1 s,邻近的周边被水包围。例如,这个矩阵有4个岛屿。
1 0 0 0 0
0 0 1 1 0
0 1 1 0 0
0 0 0 0 0
1 1 0 0 1
1 1 0 0 1

85.[Medium] Given three 32-bit integers x, y, and b, return x if b is 1 and y if b is 0, using only mathematical or bit operations. You can assume b can only be 1 or 0.
给出三个32位的整数x, y, b,返回x如果b是1,y b = 0,只使用数学或一些操作。你可以假设b只能1或0。

86.[Medium] Given a string of parentheses, write a function to compute the minimum number of parentheses to be removed to make the string valid (i.e. each open parenthesis is eventually closed).
For example, given the string “()())()”, you should return 1. Given the string “)(”, you should return 2, since we must remove all of them.
给定一个字符串括号中,编写一个函数来计算最小数量的删除括号使字符串有效(即每个打开括号最终关闭)。 例如,给定的字符串“()())()”,你应该返回1。给定的字符串”)(“,你应该返回2,因为我们必须删除所有。

87.[Hard] A rule looks like this: 规则是这样的:
A NE B
This means this means point A is located northeast of point B. 意味点 A位于B的东北部.
A SW C
means that point A is southwest of C. 意味着点 A是西南 C.
Given a list of rules, check if the sum of the rules validate. For example:
鉴于一系列规则,检查是否验证规则的总和。例如:
A N B
B NE C
C N A
does not validate, since A cannot be both north and south of C. 不验证,因为 A不能的南北 C.
A NW B
A N B
is considered valid. 被认为是有效的。

88.[Medium] Implement division of two positive integers without using the division, multiplication, or modulus operators. Return the quotient as an integer, ignoring the remainder.
实现两个正整数,没有使用部门,分工乘法或模数运算符。返回商为整数,因此忽略了其余部分。

89.[Medium] Determine whether a tree is a valid binary search tree.
A binary search tree is a tree with two children, left and right, and satisfies the constraint that the key in the left child must be less than or equal to the root and the key in the right child must be greater than or equal to the root.
确定一个树是一个有效的二叉搜索树。 一个二叉搜索树是一个树,两个孩子, left和 right,满足约束的关键 left孩子必须小于或等于的根本和关键 right孩子必须大于或等于根。

90.[Medium] Given an integer n and a list of integers l, write a function that randomly generates a number from 0 to n-1 that isn’t in l (uniform).
给定一个整数 n和一个整数列表 l写一个函数,随机生成一个数字 0来 n-1不是在 l(统一)。

91.[Easy] What does the below code snippet print out? How can we fix the anonymous functions to behave as we’d expect?
下面的代码片段打印什么?我们怎样才能解决这个匿名函数我们期望的那样吗?
functions = []
for i in range(10):
functions.append(lambda : i)
for f in functions:
print(f())

92.[Hard] We’re given a hashmap associating each courseId key with a list of courseIds values, which represents that the prerequisites of courseId are courseIds. Return a sorted ordering of courses such that we can finish all courses.
我们给定一个hashmap关联 courseId键的列表 courseIds值,代表的先决条件 courseId是 courseIds。返回一个排序顺序的课程,这样我们可以完成所有的课程。
Return null if there is no such ordering. 如果没有这样的命令返回null。
For example, given {‘CSC300’: [‘CSC100’, ‘CSC200’], ‘CSC200’: [‘CSC100’], ‘CSC100’: []}, should return [‘CSC100’, ‘CSC200’, ‘CSCS300’].
例如,给定{“CSC300”:“CSC100”、“CSC200”,“CSC200”:“CSC100”,“CSC100”:[]},应该返回[‘CSC100’,‘CSC200’, ’ CSCS300 ']

93.[Hard] Given a tree, find the largest tree/subtree that is a BST.
给定一个树,找到最大的树/子树BST。
Given a tree, return the size of the largest tree/subtree that is a BST.
给定一个树,返回的大小最大的树/子树BST。

94.[Easy] Given a binary tree of integers, find the maximum path sum between two nodes. The path must go through at least one node, and does not need to go through the root.
给定一个二叉树的整数,找到最大的两个节点之间的路径数目。路径必须经过至少一个节点,并且不需要经过根。

95.[Hard] Given a number represented by a list of digits, find the next greater permutation of a number, in terms of lexicographic ordering. If there is not greater permutation possible, return the permutation with the lowest value/ordering.
给定一个数字代表一个数字列表,找到下一个更大的一个数字,排列的词典排序。如果没有大的排列,返回最低的置换值/订购。
For example, the list [1,2,3] should return [1,3,2]. The list [1,3,2] should return [2,1,3]. The list [3,2,1] should return [1,2,3].
例如,列表 [1,2,3]应该返回 [1,3,2]。列表 [1,3,2]应该返回 [2,1,3]。列表 [3,2,1]应该返回 [1,2,3].
Can you perform the operation without allocating extra memory (disregarding the input memory)?
你能执行的操作没有分配额外的内存(无视输入内存)?

96.[Hard] Given a number in the form of a list of digits, return all possible permutations.
给定一个数字一个数字列表的形式,返回所有可能的排列。
For example, given [1,2,3], return [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]].

97.[Medium] Write a map implementation with a get function that lets you retrieve the value of a key at a particular time.
编写一个地图实现与get函数允许您检索在特定时间的一个关键的价值。
It should contain the following methods:
它应该包含以下方法:
set(key, value, time): sets key to value for t = time.
get(key, time): gets the key at t = time.
The map should work like this. If we set a key at a particular time, it will maintain that value forever or until it gets set at a later time. In other words, when we get a key at a time, it should return the value that was set for that key set at the most recent time.
地图应该像这样工作。如果我们将一个关键在一个特定的时间,它会永远保持这种价值或直到它被设置在稍后的时间。换句话说,当我们得到一个关键,它应该返回的值,设置关键设置在最近的时间。
Consider the following examples:
d.set(1, 1, 0) # set key 1 to value 1 at time 0
d.set(1, 2, 2) # set key 1 to value 2 at time 2
d.get(1, 1) # get key 1 at time 1 should be 1
d.get(1, 3) # get key 1 at time 3 should be 2
d.set(1, 1, 5) # set key 1 to value 1 at time 5
d.get(1, 0) # get key 1 at time 0 should be null
d.get(1, 10) # get key 1 at time 10 should be 1
d.set(1, 1, 0) # set key 1 to value 1 at time 0
d.set(1, 2, 0) # set key 1 to value 2 at time 0
d.get(1, 0) # get key 1 at time 0 should be 2

98.[Easy] Given a 2D board of characters and a word, find if the word exists in the grid.
给定一个2D的字符板和一个单词,查找单词是否存在于网格中。
The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
该单词可以由顺序相邻的单元格的字母构成,其中“相邻”单元格是水平或垂直相邻的单元格。 同一字母单元不得重复使用。
该单词可以由顺序相邻的单元格的字母构成,其中“相邻”单元格是水平或垂直相邻的单元格。 同一字母单元不得重复使用。
For example, given the following board:
[
[‘A’,‘B’,‘C’,‘E’],
[‘S’,‘F’,‘C’,‘S’],
[‘A’,‘D’,‘E’,‘E’]
]
exists(board, “ABCCED”) returns true, exists(board, “SEE”) returns true, exists(board, “ABCB”) returns false.

99.[Medium] Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
给定一个未排序的整数数组,找到的长度最长的连续元素序列。
For example, given [100, 4, 200, 1, 3, 2], the longest consecutive element sequence is [1, 2, 3, 4]. Return its length: 4.
例如,给定 [100, 4, 200, 1, 3, 2],最长的连续元素序列 [1, 2, 3, 4]。返回其长度: 4.
Your algorithm should run in O(n) complexity. 你的算法应该在运行 O(n)的复杂性。

100.[Easy] You are in an infinite 2D grid where you can move in any of the 8 directions:
你是在一个无限的2 d网格,您可以进入任何8方向:
(x,y) to
(x+1, y),
(x - 1, y),
(x, y+1),
(x, y-1),
(x-1, y-1),
(x+1,y+1),
(x-1,y+1),
(x+1,y-1)
You are given a sequence of points and the order in which you need to cover the points. Give the minimum number of steps in which you can achieve it. You start from the first point.
你给出的序列点和你需要支付的顺序点。给的最小数量的步骤你可以实现它。你从第一点开始. Example:
Input: [(0, 0), (1, 1), (1, 2)]
Output: 2
It takes 1 step to move from (0, 0) to (1, 1). It takes one more step to move from (1, 1) to (1, 2).
这需要从1步骤 (0, 0)来 (1, 1)。它需要一个步骤从 (1, 1)来 (1, 2).

101.[Easy] Given an even number (greater than 2), return two prime numbers whose sum will be equal to the given number.
给定的一个偶数(大于2),返回两个质数的和等于给定的数字。
A solution will always exist. See Goldbach’s conjecture.
一个解决方案将永远存在。看到哥德巴赫猜想的. Example:
Input: 4
Output: 2 + 2 = 4
If there are more than one solution possible, return the lexicographically smaller solution.
如果有一个以上的解决方案,返回按小的解决方案。
If [a, b] is one solution with a <= b, and [c, d] is another solution with c <= d, then
如果[a, b]与< = b,和一个解决方案(c, d)与c < = d,然后是另一个解决方案
[a, b] < [c, d]
If a < c OR a==c AND b < d.

102.[Medium] Given a list of integers and a number K, return which contiguous elements of the list sum to K.
给出一组整数和K,返回列表的相邻元素和K
For example, if the list is [1, 2, 3, 4, 5] and K is 9, then it should return [2, 3, 4], since 2 + 3 + 4 = 9.
例如,如果列表(1、2、3、4、5)和K是9,那么它应该返回(2、3、4),自2 + 3 + 4 = 9。

103.[Medium] Given a string and a set of characters, return the shortest substring containing all the characters in the set.
给定一个字符串和一组字符,返回最短的字符串中包含所有的人物。
For example, given the string “figehaeci” and the set of characters {a, e, i}, you should return “aeci”.
例如,给定的字符串“figehaeci”和字符的集合{a, e,我},您应该返回“aeci”。
If there is no substring containing all the characters in the set, return null.
如果没有子字符串中包含所有的人物设置,返回null。

104.[Easy] Determine whether a doubly linked list is a palindrome. What if it’s singly linked?
确定一个双向链表是一个回文。如果是单链什么? For example, 1 -> 4 -> 3 -> 4 -> 1 returns True while 1 -> 4 returns False. 例如, 1 -> 4 -> 3 -> 4 -> 1返回 True而 1 -> 4返回 False.

105.[Easy] Given a function f, and N return a debounced f of N milliseconds.
给定一个函数f,N返回去抖的f毫秒。
That is, as long as the debounced f continues to be invoked, f itself will not be called for N milliseconds.
也就是说,只要去抖动的f继续被调用,f自身将在N毫秒内不会被调用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海啦啦喽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值