《劍指offer》系列 1

2021-06-18 13:43:24 字數 3220 閱讀 3994

最近一直在看劍指offer,這上面的題目都是比較考察程式設計能力的,打算做個記錄,把寫過的**儲存下來:

1.實現乙個string類

面試官的考察點應該在以下幾點:

1.模板類的書寫

2.對於賦值函式考察的幾點:(1)是否返回引用,因為只有返回引用,才能連續的進行賦值 (2)引數是否是常量 (3)是否是自身賦值(4)是否釋放原來的記憶體 

乙個完善的 string類如下:

#include #include using namespace std;

class string

;string::string(const char *str)

}string::string(const string &rhs)

}string::~string()

string& string::operator=(const string &rhs)

return *this;*/

//摘自《劍指offer》

if(&rhs != this)

return *this;

}ostream &operator<<(ostream &os, string &str)

} return false;

}int main()

; printf("please input a number to be finded\n");

int c;

scanf("%d",&c);

if(find_in_sort_array(array, row, column, c))

printf("finded\n");

else printf("not finded\n");

return 0;

}

如果這個題目你是想著直接遍歷這個字串,然後每找到乙個空格就把這個空格後面的字元後移一位的話,估計你是得不到面試官青睞的,因為移動多次是乙個充分的過程,我們可不可以從後向前移動呢,這樣我們就需要先遍歷這個字串,然後找到空格的個數,把指標指向空格個數+2+len的位置,這樣再遍歷這個字串,從後往前把資料往後移動就可以了

**如下:

#include #include #include //這是乙個合併陣列的題目,首先要考慮地乙個陣列是否足夠大,如果不夠就要重新申請記憶體空間。如果夠的話我們從後往前合併兩個陣列到其中那個可以容納所以資料的陣列中

char *replace_blank(char *s)

char *p = s + (len+i*2);

char *q = s + len;

while(q >= s)//需要注意的細節,考慮了字串剛開始的空格

else

}return s;

}void main()

4.從尾到頭列印單鏈表

這個題目讓我想起了快速排序的遞迴和非遞迴實現,考察你對棧的本質的理解:

系統使用棧和我們自己使用棧

#include #include #include #include using namespace std;

typedef struct listnode

listnode;

void add_node(listnode **head, listnode *node)

else

}void delete_node(listnode *head, int val)

p = p->next; }}

void print_list(listnode *head)

}//考點來了,在不允許修改單鏈表結構的情況下從尾到頭列印單鏈表,怎麼辦呢?這不是乙個遞迴的問題嘛

/*void print_from_tail(listnode *head)

*/void print_from_tail(listnode *head)

while(!slist.empty()) }

int main()

print_list(head);

//delete_node(head, 4);

//delete_node(head, 2);

//print_list(head);

print_from_tail(head->next);

listnode *p = head->next, *temp;

while(p != null)

free(head);

return 0;

}

5.重建一顆二叉樹,例如,給定前序遍歷和中序遍歷,構造這樣的二叉樹

其實這個題目如果理解了,二叉樹選擇題那簡直太easy了,我們看前序遍歷第乙個數是1,這是乙個根節點,然後中序遍歷中1前面的

所有的數是左子節點,後面的所有的數是右子節點,然後在這三個左子節點中,在前序遍歷中地乙個節點是2,這是個左子樹的

根節點,然後對於中序遍歷,在2的左邊,所以是2的左子節點,然後再看前序遍歷,4是根節點,再看中序遍歷,7是右子節點

就這樣就構造出了這個二叉樹:

#include using namespace std;

class btreenode

;btreenode *construct_btree_func(int *pre_first, int *pre_last, int *in_first, int *in_last)

btreenode *construct_btree(int pre, int in, int len)

void post_visit(btreenode *root)

;template //寫錯的地方沒想到是這個格式

queue::queue()

template queue::~queue()

template t queue::delete_node()

} if(stack2.size() == 0)

;templatestack::stack()

{}templatestack::~stack()

{}templatevoid stack::insert(t const &x)

templatet stack::get()

cout<<"satck is empty"for(int i = 0; i < 5; i++)

stack.insert(i);

for(int i = 0; i< 5; i++)

cout<

《劍指offer》系列 2

1.求斐波那契數列的第n項 這個題目很簡單,講遞迴的書上都是用這個來講的,但是面試的時候,如果你寫個遞迴,那估計會讓人失望的,因為遞迴的效率真是乙個問題,你可以測試一下,輸入50,基本上得到結果的時間,夠你去喝杯茶了 include using namespace std 使用遞迴效率太低了,甚至可...

《劍指offer 題目1》

在乙個二維陣列中 每個一維陣列的長度相同 每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成乙個函式,輸入這樣的乙個二維陣列和乙個整數,判斷陣列中是否含有該整數。開始想到的是從左上角開始搜尋,這樣搜尋可能重複,可以標記去重。但是從左下角開始向右上角搜尋比較高效。測試資料集...

劍指offer陣列1

在乙個長度為n的陣列裡的所有數字都在0到n 1的範圍內。陣列中某些數字是重複的,但不知道有幾個數字是重複的。也不知道每個數字重複幾次。請找出陣列中任意乙個重複的數字。例如,如果輸入長度為7的陣列,那麼對應的輸出是第乙個重複的數字2。引數 numbers 乙個整數組 length 陣列的長度 dupl...