ARTS挑战-第二周
什么是ARTS?
1.Algorithm:每周至少做一个 leetcode 的算法题
2.Review:阅读并点评至少一篇英文技术文章
3.Tip:学习至少一个技术技巧
4.Share:分享一篇有观点和思考的技术文章
Algorithm
一开始以为这道题目挺难的,后来其实慢慢的研究下去发现还好,就是2数相加,需要考虑进位的问题。代码提交后看了一下Solution,发现思路大致也差不多。一开始还怀疑自己,没想到能抓到老鼠的都是好猫。
Q:
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
1 | Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) |
A:
1 | /** |
Review
这一篇 Pro Pattern-Matching in Swift 把 Swift 中的 Pattern[ˈpatərn]-Matching 讲得比较详细了。 Pattern-Matching 是 Swift 中一个能把代码写得整洁又安全的方式,必须掌握。
Tip
Swift 中部分高阶函数对比(以操作数组为例):map
vs flatMap
vs compactMap
vs filter
vs reduce
map
将数组中的每个元素进行一次操作
flatMap
同map,但是不会返回nil,且会对数组中的 optional
进行解包
compactMap
如果 flatMap
的回调中可能返回 optional
,则需要使用 compactMap
‘flatMap’ is deprecated: Please use compactMap(_:) for the case where closure returns an optional value
filter
顾名思义,可以按照 closure
中的标准,把数组中的一些元素过滤掉
reduce
reduce
就是可以把数组拍平,比如一个 [String]
,用 reduce
可以让它返回成 String
Share
Notes to Myself on Software Engineering,这是 Keras 的开发者 François Chollet 整理的笔记,里面有很多思考值得学习。由于写得太好了,我把它翻译了一下。