Algorithm
本周选择的算法题是:3Sum Closest
规则如下:
Given an array nums
of n integers and an integer target
, find three integers in nums
such that the sum is closest to target
. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example:
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Solution
我实现的方案:
Runtime:144 ms,快过 51.21%。
Memory:13.9 MB,低于 5.41%。
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
nums.sort()
result = nums[0] + nums[1] + nums[2]
if result == target: return result
for i in range(len(nums) - 2):
j, k = i + 1, len(nums) - 1
while j < k:
temp = nums[i] + nums[j] + nums[k]
if temp == target:
return temp
if abs(result - target) > abs(temp - target):
result = temp
if temp > target:
k -= 1
elif temp < target:
j += 1
return result
Review
The Scene Delegate In Xcode 11 And iOS 13
这是一篇介绍 Scene Delegate 的文章,接触过 iOS 13 以下版本的开发者可以很容易从这篇文章中看出和传统 App Delegate 的差别。
传统的 App Delegate 职责有哪些?
- 创建一个 Window,如果你不使用 Storyboard 的话
- 设置 Root View Controller,如果你不使用 Storyboard 的话
- 做一些全局的配置,比如日志、初始化各种服务,还有 UIAppearance 之类的工作
- 推送服务
- 监测应用生命周期的变化
基本上,App Delegate 涵盖了 UI 、服务、监控,稍不小心就会变得庞大而杂乱。
那 Scene Delegate 能带来什么呢?先看一张苹果官方的图:
该图来自于 Multiple Windows on iPad。
传统的 iPhone\iPad 应用只有一个 Window(大多数情况下),我们基本上可以把 Window 和 App 划上一个等号,如果想把某种相同类型的内容放在一个独立的控制器里展示或缓存,这一切只能在同一个 Window 里切换。
iOS 13 为了实现多窗口,引入了 Scene 的概念:
- 一个 App 可以有多个 Scene
- app switcher 基于 Scene 来切换
- App Delegate 中的一些生命周期方法移到 Scene Delegate 中
- Scene 和 Window 关联
Window 降成和 Scene 同一个级别,App 与 Scene(Window)也就是一对多的关系。
窗口之间很容易共享数据和资源(毕竟在同一个进程内),通过这种方式可以达到”应用多开“的效果,是种不错的设计思路。
概念上的东西大概就是这些了。
Tip
VS Code 里调试 python 标准库的方法:
- 在
launch.json
设置justMyCode
为false
即可
VS Code 中加载 pylint_django :
- 添加 pylint 的启动参数:
--load-plugins=pylint_django
VS Code 中禁用警告:
- 添加 pylint 的启动参数:
--disable=multiple-statements
Share
What is Module Stability in Swift and why should you care?
分享一篇介绍 Swift 模块稳定性的文章。
简单来讲就是模块的开发者不需要暴露源码,就能让编译好的框架在不同的 Swift 版本之间运行。这对 SDK 提供商是很有用的功能。