基本常用算法总结
1.二叉树的遍历算法
//前序遍历 struct binNode{ int val; binNode* left; binNode* right; binNode(int a = 0):val(a),left(nullptr),right(nullptr){} }; void preorder(binNode* root,vector<int>& res){ if(root == nullptr){ return; } res.push_back(root->val); preorder(root->left,res); preorder(root->right,res); } vector<int> preorder(binNode* root){ if(root == nullptr){ return {}; } vector<int> res; stack<binNode*> s; binNode* p = root; while(p != nullptr || !s.empty()){ while(p != nullptr){ res.push_back(p->val); s.push(p); p = p->left; } if(!s.empty()){ p = s.top(); s.pop(); p = p->right; } } return res; } //中序遍历 void inorder(binNode* root,vector<int> &res){ if(root == nullptr){ return; } inorder(root->left,res); res.push_back(root->val); inorder(root->right,res); } vector<int> inorder(binNode* root){ if(root == nullptr){ return {}; } stack<binNode*> s; vector<int> res; binNode* p = root; while(p != nullptr || !s.empty()){ while(p != nullptr){ s.push(p); p = p->left; } if(!s.empty()){ p = s.top(); s.pop(); res.push_back(p->val); p = p->right; } } return res; } //后续遍历 void postorder(binNode* root,vector<int>& res){ if(root == nullptr){ return; } postorder(root->left,res); postorder(root->right,res); res.push_back(root->val); } vector<int> postorder(binNode* root){ if(root == nullptr){ return {}; } stack<binNode*> s; s.push(root); binNode* cur = root; vector<int> res; binNode* pre; while(!s.empty()){ cur = s.top(); if(cur -> left == nullptr && cur -> right == nullptr || (pre != nullptr &&(pre == cur -> right || pre == cur ->left))) { res.push_back(cur->val); s.pop(); pre = cur; } else{ if(cur->right){ s.push(cur->right); } if(cur->left){ s.push(cur->left); } } } return res; } /*后序遍历思路: 保证根结点在左孩子和右孩子访问之后才能访问, 因此对于任一结点P,先将其入栈。如果P不存在左孩子和右孩子, 则可以直接访问它;或者P存在左孩子或者右孩子,但是其左孩子 和右孩子都已被访问过了,则同样可以直接访问该结点。若非上述 两种情况,则将P的右孩子和左孩子依次入栈,这样就保证了每次 取栈顶元素的时候,左孩子在右孩子前面被访问, 左孩子和右孩子都在根结点前面被访问。*/ //二叉树BFS vector<vector<int>> binaryBFS(binNode* root){ vector<vector<int>> res; if(root == nullptr){ return res; } queue<binNode*> q; q.push(root); while(!q.empty()){ int len = q.size(); vector<int> tmp; for(int i = 0;i < len;++i){ binNode* cur = q.front(); tmp.push_back(cur->val); q.pop(); if(cur->left){ q.push_back(cur->left); } if(cur->right){ q.push_back(cur->right); } res.push_back(tmp); } } return res; }
2、堆排序
//heapsort void siftdown(vector<int>& v,int i,int sz){ int largest = i; int l = 2 * i + 1; int r = 2 * i + 2; if(l < sz && v[l] > v[largest]){ largest = l; } if(r < sz && v[r] > v[largest]){ largest = r; } if(largest != i){ swap(v[i],v[largest]); siftdown(v,largest,sz); } } void heapsort(vector<int> &v){ if(v.empty()){ return; } for(int i = v.size() / 2;i >= 0;--i){ siftdown(v,i,v.size()); } for(int i = v.size() - 1;i >= 0;--i){ swap(v[0],v[i]); siftdown(v,0,i); } }
循环版本:
void siftdown(vector<int>& A, int i, int n) { while (1) { int l = 2 * i + 1; int r = 2 * i + 2; int smallest = i; if (l < n && A[l] < A[smallest]) { smallest = l; } if (r < n && A[r] < A[smallest]) { smallest = r; } if (smallest != i) { swap(A[i], A[smallest]); i = smallest; //siftdown(A, smallest, n); } else { break; } } }
3、strcpy,memcpy,memmove
https://www.cnblogs.com/chenyg32/p/3739564.html
//strcpy(现实版本没有内存检查) char* myStrcpy(char* dst,const char* src){ assert((dst != NULL) &&(src != NULL)); char* res = dst; while((*dst++ = *src++) != '\0'); return res; } //memcpy(现实版本没有内存检查) void* memcpy(void* dst,const void* src,size_t num){ assert((dst != NULL) && (src != NULL)); char* pdst = (char*)dst; const char* psrc = (const char*)src; if(pdst > psrc){ for(size_t i = num - 1;i >= 0;--i){ *pdst = *psrc; } } else{ for(size_t i = 0;i < num;++i){ *pdst = *psrc; } } return dst; } //memmove(现实版本有内存检查) //strStr 找到target在source中出现的问题 int strStr(const char* src,const char* tar){ if(src == nullptr || tar == nullptr){ return -1; } int srcLen = strlen(src); int tarLen = strlen(tar); for(int i = 0;i < srcLen - tarLen;++i){ in j = 0; for(j = 0;j < tarlen;++j){ if(src[i + j] != tar[j]){ break; } } if(j == tarLen){ return i; } } return -1; }
char *my_memcpy(char *dst, const char* src, int cnt) { assert(dst != NULL && src != NULL); char *ret = dst; if (dst >= src && dst <= src+cnt-1) //内存重叠,从高地址开始复制 { dst = dst+cnt-1; src = src+cnt-1; while (cnt--) *dst-- = *src--; } else //正常情况,从低地址开始复制 { while (cnt--) *dst++ = *src++; } return ret; } char * strcpy(char *dst,const char *src) { assert(dst != NULL && src != NULL); char *ret = dst; my_memcpy(dst, src, strlen(src)+1); return ret; }