摘要:一、Leetcode146.LRU缓存 1. 使用STL list和unordered_map实现 using KV = pair<int, int>; class LRUCache{ private: int cacheCapacity; list<KV> cacheList; unordered
阅读全文
摘要:单链表排序 以arr = [8,6,7,5,1,2]为例 1. 自顶向下归并排序(递归)——分治法 Time: O(NlogN) Space: O(LOG(N)) 自顶向下: (8->6->7) | (5->1->2) (8->6)|(7) | (5->1)|(2) (8)|(6)|(7) |(5)
阅读全文
摘要:一.二叉搜索树的最近公共祖先 利用二叉搜索树的性质,祖先的两个孩子,左孩子的小于根节点的值,右孩子大于根节点的值。 如果根节点的值,同时大于p的值和q的值,那么在左子树找根节点; 如果根节点的值,同时小于p的值和q的值,那么在右子树找根节点。 /** * Definition for a binar
阅读全文
摘要:一、快速排序 int getPivot(vector<int>& arr, int left, int right){ int tmp = arr[left]; while(left < right){ while(left < right && arr[right] >= tmp){ right
阅读全文
摘要:一.冒泡排序 #include <cstdio> #include <cstring> int main() { int a[10]={5,8,16,7,9,10,14,12,2,1}; int i,j; for(i=0;i<9;i++) { for(j=0;j<9-i;j++) { if(a[j]
阅读全文
摘要:图——广度优先遍历 #include <cstdio> #include <iostream> #include <queue> using namespace std; int graph[100][100]={0}; int visited[100]={0}; queue <int> q; vo
阅读全文