Algorithm
本周选择的算法题是:Average of Levels in Binary Tree。
规则
Given the root
of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5
of the actual answer will be accepted.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: [3.00000,14.50000,11.00000]
Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
Hence return [3, 14.5, 11].
Example 2:
Input: root = [3,9,20,15,7]
Output: [3.00000,14.50000,11.00000]
Constraints:
- The number of nodes in the tree is in the range
[1, 104]
. -231 <= Node.val <= 231 - 1
Solution
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn average_of_levels(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<f64> {
let mut ans = Vec::new();
let mut stack = vec![root];
while !stack.is_empty() {
let (n, mut sum) = (stack.len(), 0);
for _ in 0..n {
if let Some(node) = stack.remove(0) {
sum += i64::from(node.borrow().val);
if let Some(left) = node.borrow_mut().left.take() {
stack.push(Some(left));
}
if let Some(right) = node.borrow_mut().right.take() {
stack.push(Some(right));
}
}
}
ans.push(sum as f64 / n as f64);
}
ans
}
}
Review
自三个月前发布 Flutter 3 之后,3.3 和 3 相比又合入了 5,687 个 PRs,带来了一系列的改进:
- 在 web 侧增加了一次滑动手势选择多个元素的功能,这在 web 上很常见,但由于 flutter 体验更倾向 app,所以在之前的 flutter 版本中不存在该交互
- 对触摸板支持得更好了!
- 支持在 iPadOS 上手写输入~
- 支持 Material Design 3
- 更新了 go_router,对各个平台的导航逻辑支持的更好了
- 禁用了 iOS 指针压缩
生态系统的构建绝非一日之事,很高兴能看到 Flutter 团队在持续、高频的完善,令人尊敬。
Tip
CodeEdit 近期的热度很高,使用感受确实很快。
Share
相信大家遇到过这种场景:你正在和其他人视频开会,突然有人 cue 你 “xxx,你对这件事怎么看?”,一般这种时候我正在看对方写的文档(而不是在浏览微博),然后我需要花几秒钟找到视频会议的窗口并切换回来。
显然,这个场景可以通过自动化工具来解决。
本周的分享,我们就使用 AppleScript 一键切换到钉钉会议。
写一段 AppleScript:
- 打开 “Automator”
- 创建一个 “Quick Action”
- 在左边找到 “Run Applescript”
- “Workflow receives” 选择 “no input”
- 复制下方的脚本
- 保存
钉钉会议版本:
on run
tell application "Tblive"
activate
end tell
end run
腾讯会议版本:
on run
tell application "TencentMeeting"
activate
end tell
end run
如果你使用 Zoom,脚本会有些许的不同:
on run
tell application "zoom.us"
activate
tell application "System Events" to tell process "zoom.us" to keystroke "a" using {shift down, command down}
end tell
end run
接下来我们为它设置一个全局快捷键。
保存好后,我们打开 ”Keyboard preferences“,然后:
- 切换到 “Shortcuts”
- 在 “Services” 里找到脚本,并设置快捷键(你可以像我一样设置一个全局的功能键)
Done~