Algorithm
本周选择的算法题是:Combination Sum III。
规则
Find all valid combinations of k
numbers that sum up to n
such that the following conditions are true:
- Only numbers
1
through9
are used. - Each number is used at most once.
Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.
Example 1:
Input: k = 3, n = 7
Output: [[1,2,4]]
Explanation:
1 + 2 + 4 = 7
There are no other valid combinations.
Example 2:
Input: k = 3, n = 9
Output: [[1,2,6],[1,3,5],[2,3,4]]
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input: k = 4, n = 1
Output: []
Explanation: There are no valid combinations.
Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10 and since 10 > 1, there are no valid combination.
Example 4:
Input: k = 3, n = 2
Output: []
Explanation: There are no valid combinations.
Example 5:
Input: k = 9, n = 45
Output: [[1,2,3,4,5,6,7,8,9]]
Explanation:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
There are no other valid combinations.
Constraints:
2 <= k <= 9
1 <= n <= 60
Solution
class Solution:
def combinationSum3(self, k: int, n: int, begin = 1) -> List[List[int]]:
if k == 1:
return [[n]] if n < 10 and n >= begin else []
else:
ans = []
for i in range(begin, 10):
if k * i < n:
ans += [[i] + combination for combination in self.combinationSum3(k - 1, n - i, i + 1)]
return ans
Review
Apple’s interview questions database
作者收集了 42 个苹果面试问题,可以看出算法和设计占据了很大的篇幅,想到了本周看的另一篇文章:十年创业者,万字长文分享我是怎么招人的,同是面试/招聘话题,后文提到了 ASK 模型:
- Ability(底层软素质)
- Skill(技能)
- Knowledge(知识)
真正聪明的人主要强在 Ability,这种人学 Skill 和 Knowledge 都很快。
这也是为什么有些公司总喜欢考算法和设计题目,因为这些题目往往能考察出面试者的综合能力,回答好的话很容易在面试中取得优势。
Tip
一个学习 CSS 选择器的有趣站点~ CSS Diner
Share
iOS 10.3 以后采用的是苹果自研的 Apple File System,根据文件系统的特性,当你使用下述标准 copyItem 接口时:
let origin = URL(fileURLWithPath: "/path/to/origin")
let destination = URL(fileURLWithPath: "/path/to/destination")
do {
// Creates a clone for Apple File System volumes, or makes
// a copy immediately for other file systems.
try FileManager.default.copyItem(at: origin, to: destination)
} catch {
// ... Handle the error ...
}
其内部实际使用的是 clone 操作,由于 clone 发生在同一个 Volume 上,所以 clone 操作效率极高,并且不占用额外的磁盘空间,是严格意义上的 COW 零拷贝技术。
PS: clone 可以发生在文件和目录上。