随笔分类 - 数据结构和算法
摘要:101. 对称二叉树 bool isQ(TreeNode* root1,TreeNode* root2){ if(root1==nullptr&&root2==nullptr){ return true; } else if(root1==nullptr||root2==nullptr){ retu
阅读全文
摘要:给定一个长度为 nn 的整数数列,请你计算数列中的逆序对的数量。 逆序对的定义如下:对于数列的第 ii 个和第 jj 个元素,如果满足 i<ji<j 且 a[i]>a[j]a[i]>a[j],则其为一个逆序对;否则不是。 输入格式 第一行包含整数 nn,表示数列的长度。 第二行包含 nn 个整数,表
阅读全文
摘要:#include<bits/stdc++.h> using namespace std; const int maxn=1010; int getP(int num[],int left,int right){ int temp=num[left]; while(left<right){ while
阅读全文
摘要:最长上升子序列: #include<bits/stdc++.h> using namespace std; #define inf 0x3fffffff const int maxn=1010; int A[maxn]; int dp[maxn]; //最长上升子序列 int main(){ int
阅读全文
摘要:#include<bits/stdc++.h> using namespace std; #define inf 0x3fffffff const int maxn=1010; struct node{ int x,y; }N[maxn]; bool cmp(node a,node b){ if(a
阅读全文
摘要:一、为什么需要B树? 用二叉查找树在磁盘上存储数据会面临两个问题: 1、二叉树只有两个分支,数据量非常大的时候,树的高度会很大,不利于数据的查找 2、每次仅若读取一条信息,会造成大量空间的浪费 因此需要把树的高度降低,并且尽量是的每次磁盘读取都能够得到最多的信息 为解决这两个问题,B树应运而生 B树
阅读全文
摘要:哨兵的作用:在查找方向的尽头设置“哨兵”免去了在查找过程中每次比较后都要判断查找位置是否越界的小技巧,在总数居较多时,效率提高很大。 未使用哨兵: #include<bits/stdc++.h> using namespace std; const int maxn=1010; #define in
阅读全文
摘要:堆是一棵完全二叉树,树中每个结点的值都不小于(或不大于)其左右孩子结点的值。 堆一般使用优先队列(priority_queue)实现,而优先队列默认情况下使用的是大顶堆。 堆的两个特性: 1、结构性:用数组表示完全二叉树 2、有序性:任一结点的关键词是其子树所有结点的最大值(或最小值) 最大堆,也称
阅读全文
摘要:排序算法的稳定性: 如果一个待排序的序列中,存在2个相等的数,在排序后这2个数的相对位置保持不变,那么该排序算法是稳定的;否则是不稳定的。 稳定的意义:排序算法如果是稳定的,从一个键上排序,然后从另一个键上排序,第一个键排序的结果可以为第二个键排序所用 两个重要定理: 定理1:任意N个不同元素组成的
阅读全文
摘要:#include<bits/stdc++.h> using namespace std; const int maxn=1010; #define inf 0x3fffffff int Next[maxn]; string s1,s2; int m,n; void buildNext(){ Next
阅读全文
摘要:模板: #include<bits/stdc++.h> using namespace std; const int maxn=10; int a[maxn];//a[i]表示第i行上的皇后放于a[i]列上,假设a[3]=7:放在第3行第7列上 int cnt=0,n; bool check(int
阅读全文
摘要:#include<bits/stdc++.h> using namespace std; vector<int> pre,post,in; void toPost(int root,int inL,int inR){ if(inL>inR){ return ; } // int k=inL; int
阅读全文
摘要:一:由先序和中序,确定后序 post(0,0,n-1); void toPost(int root,int left,int right){//root为先序中根节点的下标,left和right分别为中序中子树的左右边界 if(left>right){ return ; } int i=left;
阅读全文
摘要:模板: //树状数组 解决动态前缀和问题的数据结构 //树状数组单词查询 int d[maxn],n; //返回最后一个1的下标 int lowbit(int x){ return x&(-x); } //查询 int query(int x){ int res=0; while(x){ res+=
阅读全文
摘要:第一种,先处理源点的邻接结点,再循环处理其他结点(同陈越姥姥慕课代码) int findMin(){//查找为收录结点中dist最小的点 int mind=inf; int minv=-1; for(int i=1;i<=N;i++){ if(collected[i]==false&&dist[i]
阅读全文
摘要:dfs:搜索全部解,棋盘问题,八皇后问题 bfs:用来搜索无权最短路径,最少步数,最少交换次数,因为bfs搜索过程中遇到的解一定是离根最近的,所以遇到第一个解,一定是最优解。 总结:无权图最短路径用广搜,全部解用深搜
阅读全文
摘要:#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> using namespace std; //全排列算法 求n的全排列以及排列个数 void perm(int a[],
阅读全文
摘要:dijkstra与prim比较: 两者算法思想类似,只不过前者收录的是路径上的点,后者收录的是树上的点 另外,如果图中出现很多相同边,很可能在prim所确定的树上,源点到其他点的距离不满足dijkstra的条件,即距离不是最短的。 比如下图这种情况: 若A为源点,红线所连为prim生成的最小生成树,
阅读全文
摘要:设计函数分别求两个一元多项式的乘积与和(含排序) C++代码(全指针,无引用): #include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #
阅读全文
摘要:链接:https://ac.nowcoder.com/acm/problem/14323来源:牛客网 题目描述 Hill was a clever boy,he like math very much.Today teacher give him a question. calculate N! .
阅读全文