Excel2021鼠标错位bug最简单解决方法
- 2026-09-24 08:28:21

楔子
大家好,我是张桃狮。
之前我曾经提到过Excel2021的鼠标错位bug,有时候我用鼠标点击A1单元格,实际选中可能是A2、A3……。
当时我的解法方法是将表格保存后关闭,再重新打开就可以解决。
不过这个方法太耽误功夫,当时我在网上查到一个比较玄学的解决方法。
当这个bug产生的时候,点击当前屏幕左上角那个单元格,让它完整显示。
经过我的测试,这个方法虽然好用,但是未免太过玄学了,而且鼠标bug已经产生了,再去点左上角没有完全显示的单元格,也有点强人所难了。
今天我发现一个特别简单的解决方法——调整Excel表格右下角的缩放比例,快捷键Ctrl+滑动滚轮。
原理是这样的,bug产生的原理似乎跟Excel单元格的屏幕坐标偏移有关,通过调整缩放比例,所有Excel单元格的屏幕坐标都会重新计算,反而把这个问题解决了。
也可以点击选项卡视图-缩放到选定区域,这个时候鼠标选中的单元格所在区域会自动放大400%,特别适合用来演示教学。快捷键Alt-W-G。
再点击选项卡视图-100%,就可以恢复回来了。快捷键Alt-W-J。
大家要是遇到类似的bug,也可以如法炮制。
闲话讲完,咱们继续力扣刷题学编程。
力扣501. 二叉搜索树中的众数
给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。
如果树中有不止一个众数,可以按 任意顺序 返回。
假定 BST 满足如下定义:
结点左子树中所含节点的值 小于等于 当前节点的值结点右子树中所含节点的值 大于等于 当前节点的值左子树和右子树都是二叉搜索树
示例 1:
输入:root = [1,null,2,2]输出:[2]示例 2:
输入:root = [0]输出:[0]
提示:
树中节点的数目在范围 [1, 104] 内-105 <= Node.val <= 105
进阶:你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内)
我的思路
BFS遍历为列表,用Counter的most_common()方法统计众数。
DFS中序遍历为列表,列表应该是升序排列的,需要遍历两次列表,一次统计最大重复次数,一次取出众数。
莫里斯中序遍历,遍历的同时计算众数,真正做到空间复杂度O(1)。
以上三种思路,对我来说难度都不小,尤其是最后一种。我打算参考一下之前记录的莫里斯中序遍历代码,让我独自手搓恐怕无法完成。
BFS遍历
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightfrom collections import Counterfrom collections import dequeclass Solution: def findMode(self, root: Optional[TreeNode]) -> List[int]: res=list() bfs_list=list() q=deque() q.append(root) while q: node=q.popleft() bfs_list.append(node.val) if node.left: q.append(node.left) if node.right: q.append(node.right) c=Counter(bfs_list) count=c.most_common(1)[0][1] for k , v in c.items(): if v == count: res.append(k) return res力扣提交通过,时间复杂度O(n),空间复杂度O(n)。
DFS中序遍历
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def findMode(self, root: Optional[TreeNode]) -> List[int]: result=[] def Depth_First_Search(node): if not node: return Depth_First_Search(node.left) result.append(node.val) Depth_First_Search(node.right) Depth_First_Search(root) if len(result)==1: return result count=1 max_count=1 for i in range(len(result)-1): if result[i]==result[i+1]: count += 1 else: if count>max_count: max_count=count count=1 if count>max_count: max_count=count if max_count==1: return result res=[] count=1 for i in range(len(result)-1): if result[i]==result[i+1]: count += 1 if count == max_count: res.append(result[i]) else: count = 1 return res 力扣提交通过,时间复杂度O(n),空间复杂度O(n)。
说实话,这个代码写的很烂,为了能提交通过,添加了很多if判断。
莫里斯中序遍历
这个我实在是搞不定,还是看答案吧。
力扣官方题解
方法一:中序遍历
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightfrom typing import List, Optionalclass Solution: def findMode(self, root: Optional[TreeNode]) -> List[int]: answer = [] base = None count = 0 maxCount = 0 def update(x: int) -> None: nonlocal base, count, maxCount if x == base: count += 1 else: count = 1 base = x if count == maxCount: answer.append(base) if count > maxCount: maxCount = count answer.clear() answer.append(base) def dfs(node: Optional[TreeNode]) -> None: if not node: return dfs(node.left) update(node.val) dfs(node.right) dfs(root) return answer力扣提交通过,时间复杂度O(n),空间复杂度O(h)。
这个update函数神了。
nonlocal,外层嵌套函数局部变量,不是局部变量,也不是全局变量。
不写 nonlocal,内层只能读不能改。
加上 nonlocal,修改外层函数变量。
多层嵌套,nonlocal 向上找最近一层。
如果不想用nonlocal,可以用列表代替。
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightfrom typing import List, Optionalclass Solution: def findMode(self, root: Optional[TreeNode]) -> List[int]: answer = [] # 用列表封装替代 nonlocal: [base, count, maxCount] state = [None, 0, 0] def update(x: int) -> None: base, count, maxCount=state if x == base: count += 1 else: count = 1 base = x if count == maxCount: answer.append(base) if count > maxCount: maxCount = count answer.clear() answer.append(base) state[:] = [base, count, maxCount] def dfs(node: Optional[TreeNode]) -> None: if not node: return dfs(node.left) update(node.val) dfs(node.right) dfs(root) return answer注意update函数结尾需要将列表重新覆盖。
state[:] = ... 是原地修改列表内容;如果写 state = [base,count,maxCount] 又变成局部变量赋值,外层还是不变。
b = a[:] 浅拷贝(生成新列表)a[:] = iterable 原地覆盖方法二:Morris 中序遍历
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightfrom typing import List, Optionalclass Solution: def findMode(self, root: Optional[TreeNode]) -> List[int]: answer = [] base = None count = 0 maxCount = 0 def update(x: int) -> None: nonlocal base, count, maxCount if x == base: count += 1 else: count = 1 base = x if count == maxCount: answer.append(base) if count > maxCount: maxCount = count answer.clear() answer.append(base) def morris_inorder(root: Optional[TreeNode]) -> None: current = root while current: # 如果当前节点没有左子节点,直接访问当前节点并移动到右子节点 if not current.left: update(current.val) current = current.right else: # 找到当前节点左子节点的最右节点(前驱节点) predecessor = current.left while predecessor.right and predecessor.right != current: predecessor = predecessor.right # 如果前驱节点的右指针为空,将其指向当前节点,然后移动到左子节点 if not predecessor.right: predecessor.right = current current = current.left else: # 如果前驱节点的右指针已经指向当前节点,说明左子节点已遍历完 # 恢复前驱节点的右指针,访问当前节点,然后移动到右子节点 predecessor.right = None update(current.val) current = current.right morris_inorder(root) return answer力扣提交通过,时间复杂度O(n),空间复杂度O(1)。
莫里斯中序遍历可以做到空间复杂度O(1),就是代码不好写,我只会复制粘贴来用。
我把代码发给豆包,豆包建议使用callback回调,将update作为参数传入,避免将update写死在莫里斯中序遍历中。
from typing import List, Optionalclass TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = rightclass Solution: def findMode(self, root: Optional[TreeNode]) -> List[int]: answer = [] base = None count = 0 maxCount = 0 def update(x: int) -> None: nonlocal base, count, maxCount if x == base: count += 1 else: count = 1 base = x if count == maxCount: answer.append(base) if count > maxCount: maxCount = count answer.clear() answer.append(base) # 通用Morris,接收回调 def morris_inorder(node: Optional[TreeNode], callback): cur = node while cur: if not cur.left: callback(cur.val) cur = cur.right else: pred = cur.left while pred.right and pred.right != cur: pred = pred.right if not pred.right: pred.right = cur cur = cur.left else: pred.right = None callback(cur.val) cur = cur.right # ✅把update函数作为参数传入,这就是回调传入 morris_inorder(root, update) return answer力扣提交通过,时间复杂度O(n),空间复杂度O(1)。
我是个编程爱好者,小白级别的,如果你跟我一样希望通过力扣刷题,学习各种奇妙的算法,可以关注我,大家一起学习。