算法面试题解答(八)

这一节我们来看看in-place类的问题。 什么叫in-place类问题呢?就是原地操作,也就等价于没有额外内存分配。

举例来说:

1. 有个句子,例如”This is a good book”,将单词顺序反转,变为如下的一个句子:“book good a is This”,要求in-place完成;

2. 有个单链表,链表的节点,除了next指针外,还有个random指针,指向链表中的某个元素或者NULL,要求复杂度是计算:O(n),Storage:O(1);

3. 有个字符串,将该字符串中出现的字符抽取出来;

当我们被问到一个问题的时候,我们往往假定输入的数据是不能做任何改变的,而事实往往不是这样的,有时面试官就是希望我们问出来,而不是做这样的假设。所以,“Do not make any assumption”!。

1. remove duplicate integers from an array

1. two loops, one loop is going through the whole list, the other loop is to check the element with other elements;

2. sorted the array;

3. merge-sort when doing the merge sort remove the duplicate.

Reference:http://tech-queries.blogspot.jp/2011/04/copy-linked-list-with-next-and-random.html

原文地址:https://www.cnblogs.com/whyandinside/p/2825394.html