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
Leetcode.com 2021-08-03
🟡 90.subsets-ii
🏷️ Tags
#bit_manipulation #array #backtracking
Description
Given an integer array
The solution set must not contain duplicate subsets. Return the solution in any order.
Example
🟡 90.subsets-ii
🏷️ Tags
#bit_manipulation #array #backtracking
Description
Given an integer array
nums that may contain duplicates, return all possible subsets (the power set).The solution set must not contain duplicate subsets. Return the solution in any order.
Example
Input: nums = [1,2,2]
Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
Leetcode-cn.com 2021-08-05
🟡 802.find-eventual-safe-states
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort
Description
在有向图中,以某个节点为起始节点,从该点出发,每一步沿着图中的一条有向边行走。如果到达的节点是终点(即它没有连出的有向边),则停止。
对于一个起始节点,如果从该节点出发,无论每一步选择沿哪条有向边行走,最后必然在有限步内到达终点,则将该起始节点称作是 安全 的。
返回一个由图中所有安全的起始节点组成的数组作为答案。答案数组中的元素应当按 升序 排列。
该有向图有
Example
🟡 802.find-eventual-safe-states
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort
Description
在有向图中,以某个节点为起始节点,从该点出发,每一步沿着图中的一条有向边行走。如果到达的节点是终点(即它没有连出的有向边),则停止。
对于一个起始节点,如果从该节点出发,无论每一步选择沿哪条有向边行走,最后必然在有限步内到达终点,则将该起始节点称作是 安全 的。
返回一个由图中所有安全的起始节点组成的数组作为答案。答案数组中的元素应当按 升序 排列。
该有向图有
n 个节点,按 0 到 n - 1 编号,其中 n 是 graph 的节点数。图以下述形式给出:graph[i] 是编号 j 节点的一个列表,满足 (i, j) 是图的一条有向边。Example
输入:graph = [[1,2],[2,3],[5],[0],[5],[],[]]
输出:[2,4,5,6]
解释:示意图如上。
Leetcode.com 2021-08-04
🟡 113.path-sum-ii
🏷️ Tags
#tree #depth_first_search #backtracking #binary_tree
Description
Given the
A leaf is a node with no children.
Example
🟡 113.path-sum-ii
🏷️ Tags
#tree #depth_first_search #backtracking #binary_tree
Description
Given the
root of a binary tree and an integer targetSum, return all root-to-leaf paths where each path's sum equals targetSum.A leaf is a node with no children.
Example
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output: [[5,4,11,2],[5,8,4,5]]
Leetcode-cn.com 2021-08-06
🔴 847.shortest-path-visiting-all-nodes
🏷️ Tags
#bit_manipulation #breadth_first_search #graph #dynamic_programming #bitmask
Description
存在一个由
给你一个数组
返回能够访问所有节点的最短路径的长度。你可以在任一节点开始和停止,也可以多次重访节点,并且可以重用边。
Example
🔴 847.shortest-path-visiting-all-nodes
🏷️ Tags
#bit_manipulation #breadth_first_search #graph #dynamic_programming #bitmask
Description
存在一个由
n 个节点组成的无向连通图,图中的节点按从 0 到 n - 1 编号。给你一个数组
graph 表示这个图。其中,graph[i] 是一个列表,由所有与节点 i 直接相连的节点组成。返回能够访问所有节点的最短路径的长度。你可以在任一节点开始和停止,也可以多次重访节点,并且可以重用边。
Example
输入:graph = [[1,2,3],[0],[0],[0]]
输出:4
解释:一种可能的路径为 [1,0,2,0,3]
Leetcode.com 2021-08-05
🟡 877.stone-game
🏷️ Tags
#array #math #dynamic_programming #game_theory
Description
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return
Example
🟡 877.stone-game
🏷️ Tags
#array #math #dynamic_programming #game_theory
Description
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones
piles[i].The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return
True if and only if Alex wins the game.Example
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Leetcode-cn.com 2021-08-07
🟡 457.circular-array-loop
🏷️ Tags
#array #hash_table #two_pointers
Description
存在一个不含
如果
如果
因为数组是 环形 的,所以可以假设从最后一个元素向前移动一步会到达第一个元素,而第一个元素向后移动一步会到达最后一个元素。
数组中的 循环 由长度为
遵循上述移动规则将导致重复下标序列
所有
如果
Example
🟡 457.circular-array-loop
🏷️ Tags
#array #hash_table #two_pointers
Description
存在一个不含
0 的 环形 数组 nums ,每个 nums[i] 都表示位于下标 i 的角色应该向前或向后移动的下标个数:如果
nums[i] 是正数,向前 移动 nums[i] 步如果
nums[i] 是负数,向后 移动 nums[i] 步因为数组是 环形 的,所以可以假设从最后一个元素向前移动一步会到达第一个元素,而第一个元素向后移动一步会到达最后一个元素。
数组中的 循环 由长度为
k 的下标序列 seq :遵循上述移动规则将导致重复下标序列
seq[0] -> seq[1] -> ... -> seq[k - 1] -> seq[0] -> ...所有
nums[seq[j]] 应当不是 全正 就是 全负k > 1如果
nums 中存在循环,返回 true ;否则,返回 false 。Example
输入:nums = [2,-1,1,2,2]
输出:true
解释:存在循环,按下标 0 -> 2 -> 3 -> 0 。循环长度为 3 。
Leetcode.com 2021-08-06
🟡 429.n-ary-tree-level-order-traversal
🏷️ Tags
#tree #breadth_first_search
Description
Given an n-ary tree, return the level order traversal of its nodes' values.
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
Example
🟡 429.n-ary-tree-level-order-traversal
🏷️ Tags
#tree #breadth_first_search
Description
Given an n-ary tree, return the level order traversal of its nodes' values.
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
Example
Input: root = [1,null,3,2,4,null,5,6]
Output: [[1],[3,2,4],[5,6]]
Leetcode-cn.com 2021-08-08
🟢 1137.n-th-tribonacci-number
🏷️ Tags
#memoization #math #dynamic_programming
Description
泰波那契序列 Tn 定义如下:
T0 = 0, T1 = 1, T2 = 1, 且在 n >= 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2
给你整数
Example
🟢 1137.n-th-tribonacci-number
🏷️ Tags
#memoization #math #dynamic_programming
Description
泰波那契序列 Tn 定义如下:
T0 = 0, T1 = 1, T2 = 1, 且在 n >= 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2
给你整数
n,请返回第 n 个泰波那契数 Tn 的值。Example
输入:n = 4
输出:4
解释:
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4
Leetcode.com 2021-08-07
🔴 132.palindrome-partitioning-ii
🏷️ Tags
#string #dynamic_programming
Description
Given a string
Return the minimum cuts needed for a palindrome partitioning of
Example
🔴 132.palindrome-partitioning-ii
🏷️ Tags
#string #dynamic_programming
Description
Given a string
s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of
s.Example
Input: s = "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
Leetcode-cn.com 2021-08-09
🟡 313.super-ugly-number
🏷️ Tags
#array #hash_table #math #dynamic_programming #heap_priority_queue
Description
超级丑数 是一个正整数,并满足其所有质因数都出现在质数数组
给你一个整数
题目数据保证第
Example
🟡 313.super-ugly-number
🏷️ Tags
#array #hash_table #math #dynamic_programming #heap_priority_queue
Description
超级丑数 是一个正整数,并满足其所有质因数都出现在质数数组
primes 中。给你一个整数
n 和一个整数数组 primes ,返回第 n 个 超级丑数 。题目数据保证第
n 个 超级丑数 在 32-bit 带符号整数范围内。Example
输入:n = 12, primes = [2,7,13,19]
输出:32
解释:给定长度为 4 的质数数组 primes = [2,7,13,19],前 12 个超级丑数序列为:[1,2,4,7,8,13,14,16,19,26,28,32] 。
Leetcode.com 2021-08-08
🔴 1632.rank-transform-of-a-matrix
🏷️ Tags
#greedy #union_find #graph #topological_sort #array #matrix
Description
Given an
The rank is an integer that represents how large an element is compared to other elements. It is calculated using the following rules:
The rank is an integer starting from
If two elements
If
If
If
The rank should be as small as possible.
It is guaranteed that
Example
🔴 1632.rank-transform-of-a-matrix
🏷️ Tags
#greedy #union_find #graph #topological_sort #array #matrix
Description
Given an
m x n matrix, return a new matrix answer where answer[row][col] is the rank of matrix[row][col].The rank is an integer that represents how large an element is compared to other elements. It is calculated using the following rules:
The rank is an integer starting from
1.If two elements
p and q are in the same row or column, then:If
p < q then rank(p) < rank(q)If
p == q then rank(p) == rank(q)If
p > q then rank(p) > rank(q)The rank should be as small as possible.
It is guaranteed that
answer is unique under the given rules.Example
Input: matrix = [[1,2],[3,4]]
Output: [[1,2],[2,3]]
Explanation:
The rank of matrix[0][0] is 1 because it is the smallest integer in its row and column.
The rank of matrix[0][1] is 2 because matrix[0][1] > matrix[0][0] and matrix[0][0] is rank 1.
The rank of matrix[1][0] is 2 because matrix[1][0] > matrix[0][0] and matrix[0][0] is rank 1.
The rank of matrix[1][1] is 3 because matrix[1][1] > matrix[0][1], matrix[1][1] > matrix[1][0], and both matrix[0][1] and matrix[1][0] are rank 2.
Leetcode.com 2021-08-09
🟢 415.add-strings
🏷️ Tags
#math #string #simulation
Description
Given two non-negative integers,
You must solve the problem without using any built-in library for handling large integers (such as
Example
🟢 415.add-strings
🏷️ Tags
#math #string #simulation
Description
Given two non-negative integers,
num1 and num2 represented as string, return the sum of num1 and num2 as a string.You must solve the problem without using any built-in library for handling large integers (such as
BigInteger). You must also not convert the inputs to integers directly.Example
Input: num1 = "11", num2 = "123"
Output: "134"
Leetcode-cn.com 2021-08-12
🟡 516.longest-palindromic-subsequence
🏷️ Tags
#string #dynamic_programming
Description
给你一个字符串
子序列定义为:不改变剩余字符顺序的情况下,删除某些字符或者不删除任何字符形成的一个序列。
Example
🟡 516.longest-palindromic-subsequence
🏷️ Tags
#string #dynamic_programming
Description
给你一个字符串
s ,找出其中最长的回文子序列,并返回该序列的长度。子序列定义为:不改变剩余字符顺序的情况下,删除某些字符或者不删除任何字符形成的一个序列。
Example
输入:s = "bbbab"
输出:4
解释:一个可能的最长回文子序列为 "bbbb" 。