LeetCode: Weekly Contest 92

LeetCode: Weekly Contest 92

题解列表

1. LeetCode: 868. Transpose Matrix 题解

第一题照例是签到题, 根据矩阵的转置定义,行列互换。

2.LeetCode: 866. Smallest Subtree with all the Deepest Nodes 题解

关于二叉树的题,照例用递归分治思想求解。

  1. 如果 rootnullptr,则直接返回 nullptr;
  2. 如果 root的左子树的深度等于 root 的右子树的深度,则返回 root;
  3. 如果 root 的左子树的深度大于 root 的右子树的深度,则返回 root 的左子树的 mallest Subtree with all the Deepest Nodes;
  4. 否则,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 求最近距离

猜你喜欢

转载自blog.csdn.net/yanglingwell/article/details/80960117