2.3.4 tree
Traversing: After sequence before breadth.
Binary Tree exceptions: binary search tree, heap (maximum and minimum heap heap, looking for the most value), red-black tree (c ++ STL Much of the data is based on the results of this implementation);
7- binary reconstruction problem: recursion, four sites;
class Linuxidc {
public:
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
if(pre.size()!=vin.size()||vin.size()==0) return nullptr;
return Construct(0,pre.size()-1,0,vin.size()-1,pre,vin);
}
TreeNode* Construct(int prestart,int preend,int vinstart,int vinend,const vector<int> &pre,const vector<int> &vin){
if(prestart>preend) return nullptr;
TreeNode* root=new TreeNode(pre[prestart]);
for(int i=vinstart;i<=vinend;i++)
if(pre[prestart]==vin[i]){
root->left=Construct(prestart+1,prestart+i-vinstart,vinstart,i-1,pre,vin);
root->right=Construct(prestart+i-vinstart+1,preend,i+1,vinend,pre,vin);
break;
}
return root;
}
};
The next question 8- node binary tree
class Linuxidc {
public:
TreeLinkNode* GetNext(TreeLinkNode* pNode)
{
if(pNode==nullptr) return nullptr;
if(pNode->right!=nullptr){
pNode=pNode->right;
while(pNode->left!=nullptr){
pNode=pNode->left;
}
return pNode;
}
else{
if(pNode->next==nullptr)
return nullptr;
else{
if(pNode==pNode->next->left)
return pNode->next;
else{
while(pNode->next!=nullptr){
if(pNode==pNode->next->left)
return pNode->next;
pNode=pNode->next;
}
return nullptr;
}
}
}
}
};
2.3.5 stacks and queues
9- title queue two stacks: one for the insertion, for a deletion, addition empty judgment operation, each insert / delete operations only after there is a data stack;
2.4 algorithms and data structures
Recursive / cycle, sort / search, path search (backtracking), the optimal solution (dynamic programming), greedy bit operations (AND, OR, XOR, shift left)
2.4.1 and recursive loop
If the interviewer does not require, the use of recursion; there is a question of time and efficiency of the recursive call stack overflow;
That title deed 10- Fibonacci number column: recursive low efficiency, the use of loop O (n), can be used mathematical formulas using recursive O (logn), frog jump and plaid is the problem;
2.4.2 Finding and sorting
Sequential search, binary, hash table lookup, binary sort tree
Exchange the swap function, the random number is performed with a recursive fast row;
AC interviewer: to ask what sort of data, the amount of data, the number of auxiliary space can be used? Clear Scene
11- smallest number of rotation of the array title: O (n) to O (logn), using two points, but there must be special treatment, for example, if two sites elements are equal, then the internal sequential search should be conducted;
class Linuxidc {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
if(rotateArray.size()==0) return 0;
int low=0,high=rotateArray.size()-1,mid=(low+high)/2;
if(rotateArray[low]<rotateArray[high]) return rotateArray[low];
if(high<2) return rotateArray[high];
while(low!=high-1){
if(rotateArray[low]<rotateArray[high]) return rotateArray[low];
if(rotateArray[low]==rotateArray[mid]&&rotateArray[mid]==rotateArray[high]){
int min=rotateArray[low];
for(int i=low+1;i<=high;i++)
if(min>rotateArray[i])
min=rotateArray[i];
return min;
}
if(rotateArray[low]>rotateArray[mid]){
//low=low+1;
high=mid;
mid=(low+high)/2;
}
if(rotateArray[high]<rotateArray[mid]){
low=mid;
mid=(low+high)/2;
}
}
return rotateArray[low+1];
}
};
2.4.3 Backtracking
Suitable implemented recursively, in fact, can also use the stack.
Robot motion planning problem 13-: the thought that you can find two traversing the lattice can not reach, and later found to be wrong, we still use the back to look for
class Linuxidc {
public:
int movingCount(int threshold, int rows, int cols)
{
if(threshold<=0||rows<=0||cols<=0) return 0;
bool *visited=new bool[rows*cols];
for(int i=0;i<rows*cols;i++)
visited[i]=false;
int res=getCount(rows,cols,0,0,threshold,visited);
delete[] visited;
return res;
}
int getCount(int rows,int cols,int row,int col,int threshold,bool* visited){
int count=0;
if(!visited[row*cols+col]&&(getSum(row,col)<=threshold)&&row>=0&&col>=0&&row<rows&&col<cols){
visited[row*cols+col]=true;
count=1+getCount(rows,cols,row-1,col,threshold,visited)+getCount(rows,cols,row+1,col,threshold,visited)+getCount(rows,cols,row,col-1,threshold,visited)+getCount(rows,cols,row,col+1,threshold,visited);
}
return count;
}
int getSum(int rows,int cols){
int sum=0;
while(rows>0||cols>0){
sum+=(rows%10+cols%10);
rows/=10;
cols/=10;
}
return sum;
}
};
2.4.4 dynamic programming and greedy algorithms
Regulatory action:
- Broken down into many sub-issues;
- The whole issue depends on the sub-problems
- Between the sub-problems as well as smaller overlapping subproblems
From the analysis down, from the bottom up to solve (the calculated first optimal solution sequence and stored down small problems)
Questions - cut the rope: done using dynamic programming (two cycles O (n side)); greedy method (o (1) time and space, to justify);
2.4.5 Bit Operations
AND, OR, XOR, shift left (higher computational efficiency than multiplication and division);
If it is negative attention shifted to the right, then up is 1, and after subtracting the integer 1 bit to do after the operation and the number of results equivalent to the original integer binary representation of the rightmost 1 to 0, a lot of binary questions We can use this routine;
Title - the number of binary 1
class Linuxidc {
public:
int NumberOf1(int n) {
int count=0;
while(n){
count++;
n=n&(n-1);
}
return count;
}
};