Algorithm
本周选择的算法题是:Ugly Number。
规则
An ugly number is a positive integer whose prime factors are limited to 2
, 3
, and 5
.
Given an integer n
, return true
if n
is an ugly number.
Example 1:
Input: n = 6
Output: true
Explanation: 6 = 2 × 3
Example 2:
Input: n = 1
Output: true
Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
Example 3:
Input: n = 14
Output: false
Explanation: 14 is not ugly since it includes the prime factor 7.
Constraints:
-231 <= n <= 231 - 1
Solution
impl Solution {
pub fn is_ugly(n: i32) -> bool {
if n <= 0 {
return false;
}
let mut n = n;
while n % 2 == 0 {
n /= 2;
}
while n % 3 == 0 {
n /= 3;
}
while n % 5 == 0 {
n /= 5;
}
n == 1
}
}
Review
这篇文章把 SSE 的关键特点总结的很全面了,对浏览器是如何实现 SSE 的应该也能有些输入。
开发者经常把 SSE 和 WebSocket 进行比较,简单来说:
- SSE 是单向的,只能从服务端向客户端发消息,不能反过来,如果客户端不满足于消费消息,还是只能通过标准的接口(如 XMLHttpRequest)向服务端发送消息
- SSE 背后的 EventSource API 属于 HTML5 标准,且是基于传统 HTTP 发送消息的,意味着它不依赖于服务器的特殊协作或实现,作为对比,WebSocket 需要保证全双工以及不同于 HTTP 的协议支持
- SSE 具备自动重连等能力
SSE 适用于客户端不需要向服务器发消息,且需要从服务端自动接收更新的场景。
Tip
一个通知服务的中间件: Novu。
Share
一个有趣的产品: Wolfia,背后是大名鼎鼎的 Y Combinator,这个产品定位为在研发阶段在开发者、产品经理和设计师之间快速对齐。
它的技术原理是:
- 通过一条 websocket 建立 web 页面和后端的连接
- web 页面提供点击、键盘等事件的响应支持
- 后端实际上有一台机器响应从 websocket 接收的消息,并通过 adb 与 “设备” 通信
The tech
We host headless (without GUI) android emulators with hardware acceleration running on AWS bare metal instances to get high performance. We use WebSockets to make a two-way connection between the browser and the emulator through ADB. The emulator’s GUI is displayed on the browser via an H.264 video feed, and we relay the user’s touch events back to the emulator. We use WSS to make this secure.
虽然该产品还处于非常早期的阶段(今年8月正式启动),目前也只支持 apk,但雏形已经起来了,未来也会提供对 iOS 的支持。
该产品设想如果能达到预期,我感觉对研发阶段的提效有很大帮助,可以设想一下,它很容易集成进 CI 流程,完成上传后可将 apk url 发送给任何一个人,不需要 Android 手机即可进行产品验收,甚至还能很方便的进行标注和反馈,大大减少协作成本。
虽然 iOS 不像 Android 那样有开放 adb,但随着这两年大家逆向出了用于 xcodebuild 和手机之间通信的 usbmuxd 传输数据,也搞出了 tidevice 这样的工具,甚至可以脱离 macOS,在 Linux、Windows 上也能运行,因此 Wolfia 的设想虽然有很多细节要处理,却也不是实现不了,在 web 环境下运行 app 终于指日可待了。