d*d
2 楼
我记得这个,好像没有得出完美答案
g*y
3 楼
heap to BST, 我只知道个不完美的答案,把heap sort后,依次填回heap结构中,用
successor()。
BST to heap, 有人给出答案:
public void bst2heap(int a[]) {
int n = a.length;
reverseArm(a, 0, n);
for (int i = 1; i < n / 2; i += 2)
reverseArm(a, i, n);
}
private void reverseArm(int a[], int from, int n) {
int to = from;
while (to * 2 + 2 < n)
to = to * 2 + 2;
while (to > from) {
int t = a[to];
a[to] = a[from];
a[from] = t;
from = from * 2 + 2;
to = to / 2 - 1;
}
}
这个答案有一点小bug, 就是最后的叶子有可能不对,需要检查一下。
successor()。
BST to heap, 有人给出答案:
public void bst2heap(int a[]) {
int n = a.length;
reverseArm(a, 0, n);
for (int i = 1; i < n / 2; i += 2)
reverseArm(a, i, n);
}
private void reverseArm(int a[], int from, int n) {
int to = from;
while (to * 2 + 2 < n)
to = to * 2 + 2;
while (to > from) {
int t = a[to];
a[to] = a[from];
a[from] = t;
from = from * 2 + 2;
to = to / 2 - 1;
}
}
这个答案有一点小bug, 就是最后的叶子有可能不对,需要检查一下。
相关阅读
opt用express mail寄,今天寄明天uscis有人收吗?下午的google就只code完一题,没来得及做第二题大家有没有听过这个公司?怕是骗子请问:有phD学位了,还能挂到community college吗?Google经典题目一问我总觉得monster比careerbuider管点用。。求建议,这样的背景能找到什么工作referee的job title or positionCareercup书第四版一道题的解答有错H1 transfer大概要多少钱呐有人熟悉一个叫Micro-World的公司吗?下个月毕业了,依旧没有工作无语啊google tokyo 怎么样?Don't update your monster resume everyday.问个stl的iterator问题请问一下背景调查应聘人口普查员, 考试满分, 却不给我工作, 怎么办?由于家庭暴力原因被捕过,之后案子dismiss 掉,以后最近拿到ms onsite的人多么?