![](https://img-blog.csdnimg.cn/20200217150500744.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
算法和数据结构
介绍常用算法知识和数据结构知识,以及算法设计分析思想。
Brienzz
这个作者很懒,什么都没留下…
展开
-
Dijkstra算法
邻接矩阵版【带权有向图】邻接表版原创 2022-06-15 17:45:42 · 637 阅读 · 0 评论 -
数塔问题的递归解法
#include<iostream>#include<algorithm>using namespace std;const int maxn = 1000;int f[maxn][maxn],dp[maxn][maxn];const int n = 5;//默认为5层数塔 //递归函数 int recur(int x,int y){ if(x==n) return dp[n][y]; //递归边界、即数塔最后一层的值 if(dp[x][y]!=0) retu.原创 2022-05-06 11:00:26 · 504 阅读 · 0 评论 -
图的深度优先搜索DFS(邻接矩阵和邻接表版)
邻接矩阵版#include<iostream>using namespace std;const int MAXV = 1000;//最大顶点数 const int INF = 1000000000;//设INF为一个很大的数 int n=5;//n为顶点数,此处设为5,分别为0,1,2,3,4 int G[MAXV][MAXV] = {//存储图的邻接矩阵,MAXV为最大顶点数 {INF,1,INF,INF,INF}, {INF,INF,1,1,INF}, {INF,1,原创 2022-04-20 15:03:43 · 965 阅读 · 0 评论 -
图的广度优先搜索BFS(邻接矩阵和邻接表版)
邻接矩阵版#include<iostream>#include<queue>using namespace std;//n为顶点数,MAXV为最大顶点数const int MAXV = 10;const int n=5;int G[MAXV][MAXV]={ {0,1,0,1,0}, {0,0,1,1,0}, {0,0,0,1,1}, {0,0,0,0,0}, {1,0,0,1,0}};bool inq[MAXV] = {false}; //若顶点i.原创 2022-04-16 14:44:13 · 931 阅读 · 0 评论 -
LeetCode刷题经验(个人汇总)
参考知乎用户地址:你们初刷leetcode时会怀疑自己的智商吗? - 知乎 (zhihu.com)1、千万不要为了 AC 而 AC,不是 AC 的越多就越强的,当你入门之后,更多的是要总结方法,寻找高效率的代码。2、刷 leetcode 不是一朝一夕的事情,需要一个过程。3、学习算法,应该要一步一步来,要有规划。二、如何刷题如果要说学算法的捷径,那么刷题便是最好的捷径,只有在满足一定题量的情况下,才适合来谈论所谓的技巧。分类刷题,阶段性总结。刷题也是一样,前期先分类,把每个题型掌原创 2022-03-10 09:07:41 · 7778 阅读 · 0 评论 -
leetcode刷题经验总结
队列和栈DFS就是沿着一条路径一直走下去,当遇到终止条件的时候才会返回,而BFS就是先把当前位置附近的访问一遍,就像下面这样先访问圈内的,然后再把圈放大继续访问,就像下面这样...原创 2021-08-24 16:35:52 · 669 阅读 · 0 评论 -
算法初步(一)
问题 D: 字符串内排序:gets()可以接收空格,scanf()遇到空格、回车和Tab键都会输入结束,不能接收空格,cin也是不能接收空格。scanf ("%s",str)函数读取输入缓冲的字符直到出现空格、换行字符,它将读到的字符保存至字符数组str中,但并不删除缓冲中紧接的空格与换行。...原创 2021-08-01 23:34:07 · 59 阅读 · 0 评论 -
C++标准模板库介绍
第六章 C++标准模板库前言:在C语言中很多功能需要读者自己实现,而C++语言引入了STL,其封装了很多实用的容器,可以利用它们直接调用本身封装的函数来实现很多功能。它对程序的简化有非常显著的效果。精髓:掌握用法、熟知区别。6.1vector常见用法详解vector翻译为向量,”变长数组“的叫法更容易理解。使用前添加vector头文件,即#include<vector>。除此之外,还需要添加一句using namespace std;定义:vector<typena原创 2021-04-03 22:01:00 · 179 阅读 · 0 评论 -
图——最短路径
一、Dijkstra算法(单源最短路径算法)#include<iostream>#include<algorithm>#include<vector>const int MAXV = 1000;//最大顶点数const int INF = 1000000000;//设INF为一个很大的数 using namespace std;//领接矩阵版//适用于点数不大(例如V小于1000的情况) //初始化数据 int n;//图的顶点数int G[M原创 2021-07-21 19:51:48 · 75 阅读 · 0 评论 -
大整数运算
#include<cstdio>#include<cmath>#include<cstring> #include<iostream>#include<algorithm>using namespace std;typedef long long ll;//大整数的存储 struct bign{ int d[1000]; int len; bign(){ memset(d,0,sizeof(d)); len = 0.原创 2021-04-08 22:33:45 · 77 阅读 · 0 评论 -
素数的相关知识点
#include<cstdio>#include<cmath>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;//素数的判断 bool isPrime(int n){ if(n<=1) return false; int sqr = (int)sqrt(1.0*n); for(int i=2;i<=sqr;i++){.原创 2021-04-07 20:16:31 · 173 阅读 · 0 评论 -
分数的表示和化简
#include<cstdio>#include<cmath>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;ll gcd(ll a,ll b){ return !b?a:gcd(b,a%b);}//分数的表示 //(由于分数的乘法和除法的过程中可能使分子或分母超过int型表示范围,//因此分子和分母应当使用long long型.原创 2021-04-07 17:33:57 · 308 阅读 · 0 评论 -
随机数的一个例子
#include<cstdio>#include<iostream>#include<cstdlib>#include<ctime>#include<cmath>#include<algorithm>using namespace std;int main(){ srand((unsigned)time(NULL));//生成随机数的种子,初始化随机种子 for(int i=0;i<10;i++){ p.原创 2021-04-07 09:03:44 · 152 阅读 · 0 评论 -
快速排序
#include<cstdio>#include<algorithm>using namespace std;//对区间[left,right]进行划分,即进行一趟快速排序 int Partition(int A[],int left,int right){ int temp = A[left]; //将A[left]存放至临时变量temp while(left<right){ while(A[right]>temp) right--; A[lef.原创 2021-04-06 21:53:01 · 68 阅读 · 0 评论 -
二路归并排序递归实现和非递归实现
二路归并排序递归实现#include<cstdio>#include<algorithm>using namespace std;const int maxn = 100;//将数组A的[L1,R1]与[L2,R2]区间合并为有序区间(此处L2即为R1+1) void merge(int A[],int L1,int R1,int L2,int R2){ int i=L1,j=L2; //i指向A[L1],j指向A[L2] int temp[maxn],inde原创 2021-04-06 21:00:37 · 263 阅读 · 0 评论 -
二分查找的递归算法
二分查找的递归算法#include<cstdio>#include<algorithm>using namespace std;int binarySearch(int A[],int left,int right,int key){ int mid = left + (right-left)/2; if(A[mid]==key) return mid; else if(A[mid]<key) return binarySearch(A,mid+1,right原创 2021-04-06 14:59:58 · 103 阅读 · 0 评论 -
贪心策略
贪心策略:每次迭代都是形成子问题的全局最优解,再由子问题的全局最优解构造问题的全局最优解原创 2020-02-18 22:16:14 · 160 阅读 · 0 评论 -
回溯法
回溯法的主要思想就是每次只构造解的一个分量,然后按照一定的方法评估这个部分构造解。如果一个部分构造解可以进一步构造而不会违反问题的约束,我们就接受对解的下一个分量所做的第一个合法的选择。如果无法对下一个分量进行合法的选择,就不必对剩下的任何分量再做任何选择。在这种情况下,算法进行回溯,把部分构造解的最后一个分量替换为合法的另一个选择。...原创 2020-02-17 16:39:24 · 116 阅读 · 0 评论 -
中序序列和层序序列构造一颗二叉树,并使得二叉树用先序序列输出
本篇文章以及代码参考其他博主的内容增加自己的注释完成,仅供学习交流#include<cstdio>#include<iostream>#include<queue> //引入stl中的队列 using namespace std;typedef struct node{ char data; //二叉树节点的数据域用字母表示 ...原创 2020-02-14 11:26:05 · 510 阅读 · 0 评论