Major revisions were done to almost every solution, including a number of alternate solutions added. The introductory chapters were massively expanded, as were the opening of each of the chapters under Technical Questions. In addition, 24 new questions were added.Cracking the Coding Interview, Fifth...
each disk sits on top of an even larger one). You have the following constraints: (A) Only one disk can be moved at a time. (B) A disk is
解法1:Naive算法,先对其高度,然后一层一层往上直到找到结果。 代码: 1//4.7 Least Common Ancestor2//This solution is Naive Algorithm, may timeout on very large and skewed trees.3#include <cstdio>4#include <cstring>5usingnamespacestd;67constintMAXN =10005;8//tree[x][0]: parent of node x...
The Solution that the book advice: 其实思路是类似的,但是可能C-String不能得到length这个值,所以解决办法是检查直到遇到C-String的结尾字符。'\0' 1.3设计一个移除String中重复字符的算法并且不能够利用额外的buffer。 My Solution: intremove_dupchar(char*array,intlength) { inti,j,k; for(i = 0;i < ...
1//18.5 Given a text file containing words, find the shortest distance between two words.2#include <algorithm>3#include <climits>4usingnamespacestd;56structListNode {7intval;8ListNode *next;9ListNode(int_val =0): val(_val), next(nullptr) {};10};1112classSolution {13public:14intminDist(...
再把下一个指针作为待删除指针操作即可 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * };*/classSolution {public:voiddeleteNode(ListNode*node) { ...
代码: 1//12.6 How would you test an ATM machine in a distributed banking system?2//Cannot figure out a clear picture, so I referred to the solution in the book.3//The solution in the book is not exactly a solution, but some tips and instrcutions on carrying out the conversation with...
classSolution {public:stringreplaceSpaces(stringS,intlength) {intfillIdx = S.size()-1;inti =0;for( i = length-1; i >=0; i--) {if(S[i] =='') { S[fillIdx]='0'; S[fillIdx-1] ='2'; S[fillIdx-2] ='%'; fillIdx-=3; ...
题目:给定一个单链表,找出倒数第K个节点。 解法:让一个指针先走K步,然后俩指针一起走到尽头。当然也可以先走到尽头数出链表的长度,然后第二次少走K步。其实耗费的工夫是一样的,但貌似总有人觉得第一种方法很巧妙很优美。 代码: 1//2.2 Remove a node from middle of a linked list2#include <cstdio>3...
67classSolution {8public:9voidmaxSubsquare(constvector<vector<int> > &matrix,int&max_left,int&max_top,int&max_size) {10intn =matrix.size();1112max_left = max_top = max_size = -1;1314if(n <=1) {15return;16}1718vector<vector<int> > top (n, vector<int>(n));19vector<vector...