LeetCode: Weekly Contest 92
题解列表
1. LeetCode: 868. Transpose Matrix 题解
第一题照例是签到题, 根据矩阵的转置定义,行列互换。
2.LeetCode: 866. Smallest Subtree with all the Deepest Nodes 题解
关于二叉树的题,照例用递归分治思想求解。
- 如果
root
为nullptr
,则直接返回nullptr
;- 如果
root
的左子树的深度等于root
的右子树的深度,则返回root
;- 如果
root
的左子树的深度大于root
的右子树的深度,则返回root
的左子树的 mallest Subtree with all the Deepest Nodes;- 否则,
root
的左子树的深度小于root
的右子树的深度,则返回root
的右子树的 mallest Subtree with all the Deepest Nodes;
3. LeetCode: 867. Prime Palindrome 题解
暴力求解 + 数学剪枝
回文数可以表示为
a[n-1]*10^(n-1) + a[n-2]*10^(n-2) + ... + a[0]*10^0
,
而a[n-1] = a[0]
因此,原式可表示为a[n-1]*(10^(n-1) + 10^0) + a[n-2]*(10^(n-2) + 10^2) + ...
,当
n
为偶数时, 原式为a[n-1]*(10^(n-1) + 10^0) + a[n-2]*(10^(n-2) + 10^2) + ... + a[n/2]*(10^(n/2) + 10^(n/2-1))
。
而(10^(n-k-1) + 10^k) = (10^(n-k-2) - 10^(n-k-3) + ... + 10^k) * 11
, 因此,当n
为偶数时, 原式能被11
整除。
4.LeetCode: 865. Shortest Path to Get All Keys 题解
DFS 遍历顺序, BFS 求最近距离