Linked list 和 Recursion

2016-02-21
阅读 2 分钟
2.1k
正如书里所说,LinkedList 和 Tree 结构都是非常经典的recursive data structure。这也就决定了他们很多operation都可以用recursion来完成。这里头我们先去探讨Linked list以及关于它的一道非常经典的问题。

Two Sum问题

2016-02-21
阅读 2 分钟
2.4k
第一个应该想到的方法是暴力解法,N integer就是N重循环,这是unacceptable solution,但是it's natural

Recursion & Binary Tree

2016-02-12
阅读 1 分钟
2k
{代码...}

一点recursion

2016-02-12
阅读 4 分钟
1.8k
第一我们要正着想,如何把现有规模为N的问题split成N - 1的问题。这里头一般有两种思路,第一种是我们不断从N decrement到N-1,第二种是我们不断从0 increment,直到N。

Count and Say

2016-02-12
阅读 2 分钟
2.1k
这道题自己之前做都是用的iteration的方法做,但是今天做了另一道题print numbers by recursion, 才想起来这道题完全可以用recursion来做。因为自己最近在锻炼写recursion,所以但凡可以用recursion来解决的问题我都会把思路详细地写下来。

Print Numbers by Recursion

2016-02-12
阅读 2 分钟
2k
这道题要求我们递归的层数不超过N层,根据这个限制条件我们必须在每一层内把当前这一层的数全部枚举出来。也就是说我们在每一层都需要把N digits的数列举出来。通过观察example可以发现:每一层的数是建立在之前一层的基础上的,也就是我们把上一层的每个数拿出来乘以10,然后分别加上0-9,就可以形成这一层的数。比如上...

Interval Partitioning Problem

2016-02-05
阅读 4 分钟
3.2k
The essence of interval partitioning problem is that we want to fit all the intervals into as few rooms as possible so that none of them overlap with others in the same room.