Leetcode-cn.com 2021-07-22
🟡 138.copy-list-with-random-pointer
🏷️ Tags
#hash_table #linked_list
Description
给你一个长度为
构造这个链表的 深拷贝。 深拷贝应该正好由
例如,如果原链表中有
返回复制链表的头节点。
用一个由
你的代码 只 接受原链表的头节点
Example
🟡 138.copy-list-with-random-pointer
🏷️ Tags
#hash_table #linked_list
Description
给你一个长度为
n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。构造这个链表的 深拷贝。 深拷贝应该正好由
n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。例如,如果原链表中有
X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random --> y 。返回复制链表的头节点。
用一个由
n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:val:一个表示 Node.val 的整数。random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为 null 。你的代码 只 接受原链表的头节点
head 作为传入参数。Example
输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]
Leetcode.com 2021-07-21
🟡 838.push-dominoes
🏷️ Tags
#two_pointers #string #dynamic_programming
Description
There are
After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.
When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.
For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.
You are given a string
Return a string representing the final state.
Example
🟡 838.push-dominoes
🏷️ Tags
#two_pointers #string #dynamic_programming
Description
There are
n dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right.After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.
When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.
For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.
You are given a string
dominoes representing the initial state where:dominoes[i] = 'L', if the ith domino has been pushed to the left,dominoes[i] = 'R', if the ith domino has been pushed to the right, anddominoes[i] = '.', if the ith domino has not been pushed.Return a string representing the final state.
Example
Input: dominoes = "RR.L"
Output: "RR.L"
Explanation: The first domino expends no additional force on the second domino.
Leetcode.com 2021-07-22
🟡 915.partition-array-into-disjoint-intervals
🏷️ Tags
#array
Description
Given an array
Every element in
Return the length of
Example
🟡 915.partition-array-into-disjoint-intervals
🏷️ Tags
#array
Description
Given an array
nums, partition it into two (contiguous) subarrays left and right so that:Every element in
left is less than or equal to every element in right.left and right are non-empty.left has the smallest possible size.Return the length of
left after such a partitioning. It is guaranteed that such a partitioning exists.Example
Input: nums = [5,0,3,8,6]
Output: 3
Explanation: left = [5,0,3], right = [8,6]
Leetcode-cn.com 2021-07-24
🟢 1736.latest-time-by-replacing-hidden-digits
🏷️ Tags
#string
Description
给你一个字符串
有效的时间为
替换
Example
🟢 1736.latest-time-by-replacing-hidden-digits
🏷️ Tags
#string
Description
给你一个字符串
time ,格式为 hh:mm(小时:分钟),其中某几位数字被隐藏(用 ? 表示)。有效的时间为
00:00 到 23:59 之间的所有时间,包括 00:00 和 23:59 。替换
time 中隐藏的数字,返回你可以得到的最晚有效时间。Example
输入:time = "2?:?0"
输出:"23:50"
解释:以数字 '2' 开头的最晚一小时是 23 ,以 '0' 结尾的最晚一分钟是 50 。
Leetcode.com 2021-07-23
🟡 814.binary-tree-pruning
🏷️ Tags
#tree #depth_first_search #binary_tree
Description
Given the
A subtree of a node
Example
🟡 814.binary-tree-pruning
🏷️ Tags
#tree #depth_first_search #binary_tree
Description
Given the
root of a binary tree, return the same tree where every subtree (of the given tree) not containing a 1 has been removed.A subtree of a node
node is node plus every node that is a descendant of node.Example
Input: root = [1,null,0,0,1]
Output: [1,null,0,null,1]
Explanation:
Only the red nodes satisfy the property "every subtree not containing a 1".
The diagram on the right represents the answer.
Leetcode-cn.com 2021-07-25
🟡 1743.restore-the-array-from-adjacent-pairs
🏷️ Tags
#array #hash_table
Description
存在一个由
给你一个二维整数数组
题目数据保证所有由元素
返回 原始数组
Example
🟡 1743.restore-the-array-from-adjacent-pairs
🏷️ Tags
#array #hash_table
Description
存在一个由
n 个不同元素组成的整数数组 nums ,但你已经记不清具体内容。好在你还记得 nums 中的每一对相邻元素。给你一个二维整数数组
adjacentPairs ,大小为 n - 1 ,其中每个 adjacentPairs[i] = [ui, vi] 表示元素 ui 和 vi 在 nums 中相邻。题目数据保证所有由元素
nums[i] 和 nums[i+1] 组成的相邻元素对都存在于 adjacentPairs 中,存在形式可能是 [nums[i], nums[i+1]] ,也可能是 [nums[i+1], nums[i]] 。这些相邻元素对可以 按任意顺序 出现。返回 原始数组
nums 。如果存在多种解答,返回 其中任意一个 即可。Example
输入:adjacentPairs = [[2,1],[3,4],[3,2]]
输出:[1,2,3,4]
解释:数组的所有相邻元素对都在 adjacentPairs 中。
特别要注意的是,adjacentPairs[i] 只表示两个元素相邻,并不保证其 左-右 顺序。
Leetcode.com 2021-07-24
🔴 126.word-ladder-ii
🏷️ Tags
#breadth_first_search #hash_table #string #backtracking
Description
A transformation sequence from word
Every adjacent pair of words differs by a single letter.
Every
Given two words,
Example
🔴 126.word-ladder-ii
🏷️ Tags
#breadth_first_search #hash_table #string #backtracking
Description
A transformation sequence from word
beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:Every adjacent pair of words differs by a single letter.
Every
si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.sk == endWordGiven two words,
beginWord and endWord, and a dictionary wordList, return all the shortest transformation sequences from beginWord to endWord, or an empty list if no such sequence exists. Each sequence should be returned as a list of the words [beginWord, s1, s2, ..., sk].Example
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output: [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]
Explanation: There are 2 shortest transformation sequences:
"hit" -> "hot" -> "dot" -> "dog" -> "cog"
"hit" -> "hot" -> "lot" -> "log" -> "cog"
Leetcode-cn.com 2021-07-26
🔴 1713.minimum-operations-to-make-a-subsequence
🏷️ Tags
#greedy #array #hash_table #binary_search
Description
给你一个数组
每一次操作中,你可以在
请你返回 最少 操作次数,使得
一个数组的 子序列 指的是删除原数组的某些元素(可能一个元素都不删除),同时不改变其余元素的相对顺序得到的数组。比方说,
Example
🔴 1713.minimum-operations-to-make-a-subsequence
🏷️ Tags
#greedy #array #hash_table #binary_search
Description
给你一个数组
target ,包含若干 互不相同 的整数,以及另一个整数数组 arr ,arr 可能 包含重复元素。每一次操作中,你可以在
arr 的任意位置插入任一整数。比方说,如果 arr = [1,4,1,2] ,那么你可以在中间添加 3 得到 [1,4,3,1,2] 。你可以在数组最开始或最后面添加整数。请你返回 最少 操作次数,使得
target 成为 arr 的一个子序列。一个数组的 子序列 指的是删除原数组的某些元素(可能一个元素都不删除),同时不改变其余元素的相对顺序得到的数组。比方说,
[2,7,4] 是 [4,2,3,7,2,1,4] 的子序列(加粗元素),但 [2,4,2] 不是子序列。Example
输入:target = [5,1,3], arr = [9,4,2,3,4]
输出:2
解释:你可以添加 5 和 1 ,使得 arr 变为 [5,9,4,1,2,3,4] ,target 为 arr 的子序列。
Leetcode.com 2021-07-25
🔴 600.non-negative-integers-without-consecutive-ones
🏷️ Tags
#dynamic_programming
Description
Given a positive integer
Example
🔴 600.non-negative-integers-without-consecutive-ones
🏷️ Tags
#dynamic_programming
Description
Given a positive integer
n, return the number of the integers in the range [0, n] whose binary representations do not contain consecutive ones.Example
Input: n = 5
Output: 5
Explanation:
Here are the non-negative integers <= 5 with their corresponding binary representations:
0 : 0
1 : 1
2 : 10
3 : 11
4 : 100
5 : 101
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
Leetcode-cn.com 2021-07-27
🟢 671.second-minimum-node-in-a-binary-tree
🏷️ Tags
#tree #depth_first_search #binary_tree
Description
给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为
更正式地说,
给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。
Example
🟢 671.second-minimum-node-in-a-binary-tree
🏷️ Tags
#tree #depth_first_search #binary_tree
Description
给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为
2 或 0。如果一个节点有两个子节点的话,那么该节点的值等于两个子节点中较小的一个。更正式地说,
root.val = min(root.left.val, root.right.val) 总成立。给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。
Example
输入:root = [2,2,5,null,null,5,7]
输出:5
解释:最小的值是 2 ,第二小的值是 5 。
Leetcode.com 2021-07-27
🟡 16.3sum-closest
🏷️ Tags
#array #two_pointers #sorting
Description
Given an array
Example
🟡 16.3sum-closest
🏷️ Tags
#array #two_pointers #sorting
Description
Given an array
nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.Example
Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Leetcode.com 2021-07-28
🟡 932.beautiful-array
🏷️ Tags
#array #math #divide_and_conquer
Description
For some fixed
For every
Given
Example
🟡 932.beautiful-array
🏷️ Tags
#array #math #divide_and_conquer
Description
For some fixed
n, an array nums is beautiful if it is a permutation of the integers 1, 2, ..., n, such that:For every
i < j, there is no k with i < k < j such that nums[k] * 2 = nums[i] + nums[j].Given
n, return any beautiful array nums. (It is guaranteed that one exists.)Example
Input: n = 4
Output: [2,1,4,3]
Leetcode-cn.com 2021-07-30
🟢 171.excel-sheet-column-number
🏷️ Tags
#math #string
Description
给定一个Excel表格中的列名称,返回其相应的列序号。
例如,
Example
🟢 171.excel-sheet-column-number
🏷️ Tags
#math #string
Description
给定一个Excel表格中的列名称,返回其相应的列序号。
例如,
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Example
输入: "A"
输出: 1
Leetcode.com 2021-07-29
🟡 542.01-matrix
🏷️ Tags
#breadth_first_search #array #dynamic_programming #matrix
Description
Given an
The distance between two adjacent cells is
Example
🟡 542.01-matrix
🏷️ Tags
#breadth_first_search #array #dynamic_programming #matrix
Description
Given an
m x n binary matrix mat, return the distance of the nearest 0 for each cell.The distance between two adjacent cells is
1.Example
Input: mat = [[0,0,0],[0,1,0],[0,0,0]]
Output: [[0,0,0],[0,1,0],[0,0,0]]
Leetcode-cn.com 2021-07-31
🔴 987.vertical-order-traversal-of-a-binary-tree
🏷️ Tags
#tree #depth_first_search #breadth_first_search #hash_table #binary_tree
Description
给你二叉树的根结点
对位于
二叉树的 垂序遍历 从最左边的列开始直到最右边的列结束,按列索引每一列上的所有结点,形成一个按出现位置从上到下排序的有序列表。如果同行同列上有多个结点,则按结点的值从小到大进行排序。
返回二叉树的 垂序遍历 序列。
Example
🔴 987.vertical-order-traversal-of-a-binary-tree
🏷️ Tags
#tree #depth_first_search #breadth_first_search #hash_table #binary_tree
Description
给你二叉树的根结点
root ,请你设计算法计算二叉树的 垂序遍历 序列。对位于
(row, col) 的每个结点而言,其左右子结点分别位于 (row + 1, col - 1) 和 (row + 1, col + 1) 。树的根结点位于 (0, 0) 。二叉树的 垂序遍历 从最左边的列开始直到最右边的列结束,按列索引每一列上的所有结点,形成一个按出现位置从上到下排序的有序列表。如果同行同列上有多个结点,则按结点的值从小到大进行排序。
返回二叉树的 垂序遍历 序列。
Example
输入:root = [3,9,20,null,null,15,7]
输出:[[9],[3,15],[20],[7]]
解释:
列 -1 :只有结点 9 在此列中。
列 0 :只有结点 3 和 15 在此列中,按从上到下顺序。
列 1 :只有结点 20 在此列中。
列 2 :只有结点 7 在此列中。
Leetcode.com 2021-07-30
🟡 677.map-sum-pairs
🏷️ Tags
#design #trie #hash_table #string
Description
Implement the
Example
🟡 677.map-sum-pairs
🏷️ Tags
#design #trie #hash_table #string
Description
Implement the
MapSum class:MapSum() Initializes the MapSum object.void insert(String key, int val) Inserts the key-val pair into the map. If the key already existed, the original key-value pair will be overridden to the new one.int sum(string prefix) Returns the sum of all the pairs' value whose key starts with the prefix.Example
Input
["MapSum", "insert", "sum", "insert", "sum"]
[[], ["apple", 3], ["ap"], ["app", 2], ["ap"]]
Output
[null, null, 3, null, 5]
Explanation
MapSum mapSum = new MapSum();
mapSum.insert("apple", 3);
mapSum.sum("ap"); // return 3 (apple = 3)
mapSum.insert("app", 2);
mapSum.sum("ap"); // return 5 (apple + app = 3 + 2 = 5)
Leetcode-cn.com 2021-08-01
🟢 1337.the-k-weakest-rows-in-a-matrix
🏷️ Tags
#array #binary_search #matrix #sorting #heap_priority_queue
Description
给你一个大小为
请你返回矩阵中战斗力最弱的
如果第 i 行的军人数量少于第 j 行,或者两行军人数量相同但 i 小于 j,那么我们认为第 i 行的战斗力比第 j 行弱。
军人 总是 排在一行中的靠前位置,也就是说 1 总是出现在 0 之前。
Example
🟢 1337.the-k-weakest-rows-in-a-matrix
🏷️ Tags
#array #binary_search #matrix #sorting #heap_priority_queue
Description
给你一个大小为
m * n 的矩阵 mat,矩阵由若干军人和平民组成,分别用 1 和 0 表示。请你返回矩阵中战斗力最弱的
k 行的索引,按从最弱到最强排序。如果第 i 行的军人数量少于第 j 行,或者两行军人数量相同但 i 小于 j,那么我们认为第 i 行的战斗力比第 j 行弱。
军人 总是 排在一行中的靠前位置,也就是说 1 总是出现在 0 之前。
Example
输入:mat =
[[1,1,0,0,0],
[1,1,1,1,0],
[1,0,0,0,0],
[1,1,0,0,0],
[1,1,1,1,1]],
k = 3
输出:[2,0,3]
解释:
每行中的军人数目:
行 0 -> 2
行 1 -> 4
行 2 -> 1
行 3 -> 2
行 4 -> 5
从最弱到最强对这些行排序后得到 [2,0,3,1,4]
Leetcode.com 2021-07-31
🔴 42.trapping-rain-water
🏷️ Tags
#stack #array #two_pointers #dynamic_programming #monotonic_stack
Description
Given
Example
🔴 42.trapping-rain-water
🏷️ Tags
#stack #array #two_pointers #dynamic_programming #monotonic_stack
Description
Given
n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.Example
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
Leetcode-cn.com 2021-08-02
🟡 743.network-delay-time
🏷️ Tags
#depth_first_search #breadth_first_search #graph #shortest_path #heap_priority_queue
Description
有
给你一个列表
现在,从某个节点
Example
🟡 743.network-delay-time
🏷️ Tags
#depth_first_search #breadth_first_search #graph #shortest_path #heap_priority_queue
Description
有
n 个网络节点,标记为 1 到 n。给你一个列表
times,表示信号经过 有向 边的传递时间。 times[i] = (ui, vi, wi),其中 ui 是源节点,vi 是目标节点, wi 是一个信号从源节点传递到目标节点的时间。现在,从某个节点
K 发出一个信号。需要多久才能使所有节点都收到信号?如果不能使所有节点收到信号,返回 -1 。Example
输入:times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
输出:2
Leetcode.com 2021-08-01
🔴 827.making-a-large-island
🏷️ Tags
#depth_first_search #breadth_first_search #union_find #array #matrix
Description
You are given an
Return the size of the largest island in
An island is a 4-directionally connected group of
Example
🔴 827.making-a-large-island
🏷️ Tags
#depth_first_search #breadth_first_search #union_find #array #matrix
Description
You are given an
n x n binary matrix grid. You are allowed to change at most one 0 to be 1.Return the size of the largest island in
grid after applying this operation.An island is a 4-directionally connected group of
1s.Example
Input: grid = [[1,0],[0,1]]
Output: 3
Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
Leetcode-cn.com 2021-08-04
🟡 611.valid-triangle-number
🏷️ Tags
#greedy #array #two_pointers #binary_search #sorting
Description
给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。
Example
🟡 611.valid-triangle-number
🏷️ Tags
#greedy #array #two_pointers #binary_search #sorting
Description
给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。
Example
输入: [2,2,3,4]
输出: 3
解释:
有效的组合是:
2,3,4 (使用第一个 2)
2,3,4 (使用第二个 2)
2,2,3