2012年12月9日
摘要: Cracking Interview 4-3:struct Node{ int value; Node *pLeft; Node *pRight;};Node *CreateBTree(int arr[], int begin, int end){ if (!arr || (begin > end)) return NULL; Node *pNode = new Node(); int mid = (begin + end)/2; pNode->value = arr[mid]; pNode->pLeft = CreateBTree... 阅读全文
posted @ 2012-12-09 14:01 kkmm 阅读(169) 评论(0) 推荐(0) 编辑
摘要: 二叉树平衡的定义:从根到任意一个叶子节点的距离相差不超过1。#include <iostream>#include <stack>using namespace std;struct Node{ int value; Node *pLeft; Node *pRight;};int MaxDepth(Node *pRoot){ if (!pRoot) return 0; return (MaxDepth(pRoot->pLeft) > MaxDepth(pRoot->pRight)) ? (MaxDepth(pRoot->pLeft + 1)) : 阅读全文
posted @ 2012-12-09 13:10 kkmm 阅读(217) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>#include <stack>using namespace std;class MyQueue{public: void push(int value); void pop(); int front(); int back(); bool empty();private: stack<int> stackPush; stack<int> stackPop;};void MyQueue::push(int value){ stackPush.push(value);}void MyQueue::pop(){ 阅读全文
posted @ 2012-12-09 12:45 kkmm 阅读(717) 评论(0) 推荐(0) 编辑
摘要: Cracking Interview 3-6用的书上的思路,O(n^2)的时间复杂度。#include <iostream>#include <stack>using namespace std;stack<int> sort(stack<int> unorderStack){ stack<int> orderStack; stack<int> tmpStack; while(!unorderStack.empty()) { int value = unorderStack.top(); if (orderStack.em 阅读全文
posted @ 2012-12-09 12:13 kkmm 阅读(712) 评论(0) 推荐(0) 编辑
摘要: 此题目来自于Crack the Coding Interview 3-23 2 How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and in should all operate in O(1) time.思路是弄2个stack,一个realStack,存放真正的数据;另外一个是minStack,对于minStack元素中的每一个元素的意义是:push到该位置的时候,当前最小元 阅读全文
posted @ 2012-12-09 11:49 kkmm 阅读(1149) 评论(1) 推荐(1) 编辑
摘要: 此题来自于Crack the Coding Interview,3.1题。要求:用1个数组实现3个stack,使得在数组没有满的情况之下,一直可以继续插入数,而且这3个stack的push和pop操作应该和普通stack用起来没有区别。此题的思路是,在这个数组中的每一个元素中加入一个指针域,在code中,这就是pPre,就是指向更先入栈的元素。这样,整个数组除了靠数组本身的index组织起来,在已经用过的元素中,还有3条链。此外,对于pop操作,如果该pop操作不是在数组有效元素的最末一个元素进行(这是可能的,例如stack 1占用位置124,stack 2占用位置356,那么stack 1进 阅读全文
posted @ 2012-12-09 11:18 kkmm 阅读(709) 评论(0) 推荐(0) 编辑
摘要: 转自:http://blog.csdn.net/onlyou930/article/details/6725051说来惭愧,写C++有一段时间了。这个问题从来没有认真考虑过,此次标记于此:考虑如下问题:1 char a[20];2 int *ptr = (int *)a;3 ptr++;第3句ptr++实际为ptr右移一个int空间(即4个字节)的距离,此时ptr指向a[4]。若第3句改为int *p = ptr + 2;则p指向a[8]这里说明当指针加整数时,指针向后偏移的距离看的是声明该指针的类型(此处即int),而非指向的实际类型(此处即char)!!!另外,如果我就是想在第3句处得到a 阅读全文
posted @ 2012-12-09 11:02 kkmm 阅读(719) 评论(0) 推荐(0) 编辑