problem11-15
声明
该系列文章来自:http://aperiodic.net/phil/scala/s-99/
大部分内容和原文相同,加入了部分自己的代码。
如有侵权,请及时联系本人。本人将立即删除相关内容。
P11 (*) Modified run-length encoding.
要求
Modify the result of problem P10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as (N, E) terms.
Example:
|
|
方案
- (1) P10中返回的List元素类型是二元组(count,element),对list执行map判断 t._1是否为1
|
|
P12 (**) Decode a run-length encoded list.
要求
Given a run-length code list generated as specified in problem P10, construct its uncompressed version.
Example:
|
|
方案
- (1) List.fill + flatMap
|
|
P13 (**) Run-length encoding of a list (direct solution).
要求
Implement the so-called run-length encoding data compression method directly. I.e. don’t use other methods you’ve written (like P09’s pack); do all the work directly.
Example:
|
|
方案
- (1) span + 递归
|
|
P14 (*) Duplicate the elements of a list.
要求
Example:
|
|
方案
|
|
P15 (**) Duplicate the elements of a list a given number of times.
要求
Example:
|
|
方案
|
|