![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法笔记
文章平均质量分 62
lorde_mimi
这个作者很懒,什么都没留下…
展开
-
图专题
图专题图的储存图的遍历1.DFS 版本2.BFS版本最短路径Dijkstra算法图的储存邻接矩阵略邻接表struct Node{ int v; //边的终点编号 int w; //边权 Node(int _v, int _w) : v(_v), w(_w) {} //构造函数 };vector<Node> Adj[N]; Adj[1].p...原创 2019-09-03 10:27:35 · 103 阅读 · 0 评论 -
二叉树专题
二叉树专题二叉树的存储与基本操作二叉树的遍历先序遍历中序遍历后序遍历层次遍历利用先序遍历和中序遍历构造二叉树二叉树的静态实现二叉树的存储与基本操作定义struct node{ typename data; node* lchild; node* rchild;}; 新建结点node* newnode(int v){ node* Node = new node; No...原创 2019-09-03 22:07:37 · 165 阅读 · 0 评论 -
二叉查找树(BST)专题
二叉查找树专题二叉查找树的基本操作查找插入删除二叉查找树的性质二叉查找树的基本操作查找void search(node* root, int x){ if(root == NULL){ printf("search failed\n"); return; } if(root->data == x) printf("%d", x); else if(x < ro...原创 2019-09-04 10:02:59 · 130 阅读 · 0 评论 -
平衡二叉树(AVL树)
平衡二叉树树专题存储基本操作插入定义:左右子树高度之差的绝对值不超过1。存储struct node{ int data, height; node* lchild, rchild;}; //新建结点 node* newnode(int v){ node* Node = new node; Node->data = v; Node->lchild = Node-&...原创 2019-09-04 10:49:49 · 75 阅读 · 0 评论 -
并查集
并查集专题基本操作初始化查找父亲合并基本操作初始化for (int i = 1; i <= n; i++){ father[i] = i; } 查找父亲//查找(路径压缩版)int find(int x){ int a = x; while(x != father[x]){ x = father[x]; } while(a != father[a]){ i...原创 2019-09-04 11:06:08 · 71 阅读 · 0 评论 -
堆的基本操作与堆排序
堆专题基本操作向下调整向上调整堆排序堆是一颗完全二叉树,树中每个结点的值都不小于(或不大于)其左右孩子的值。基本操作向下调整完全二叉树叶子结点的个数为n/2(向上取整),在[1,n/2]范围内为非叶子结点。倒着枚举能够保证每个结点都是以其为根结点的子树中权值最大的结点,父结点一旦发现子结点的权值都比自己小就可以终止向下调整,这样downadj()才是合理的。//high一般为堆最后一个元...原创 2019-09-04 11:52:18 · 98 阅读 · 0 评论 -
静态链表
静态链表问题通用解题模板定义静态链表struct Node{ typename data; int next; XXX;}node[size]; //使用静态链表时,结构体类型名和结构体变量名不要相同初始化XXX初始化为正常情况下达不到的数字,比如:for(int i = 0; i < maxn; i++){ node[i].XXX = 0;}遍历并对...原创 2019-09-04 15:23:38 · 88 阅读 · 0 评论 -
malloc、new、free、delete的使用
malloc和new的使用malloc函数new运算符free函数delete运算符malloc函数malloc函数是C语言中stdlib.h头文件下用于申请动态内存的函数,返回类型是申请的同变量类型的指针。typename* p = (typename*)malloc(sizeof(typename));new运算符new是C++中用来申请动态空间的运算符,返回类型是申请的同变量类型...原创 2019-09-04 15:05:56 · 77 阅读 · 0 评论 -
C++标准模板库(STL)常见容器的用法
STL常见容器的用法vector定义访问元素常见函数set定义访问元素常用函数string初始化访问元素常用函数vector特点:变长数组使用vector,需要添加vector头文件,并写上using namespace std; 其他容器类似,都需要添加相应的头文件,不再赘述。定义vector<typename> name;typename可以是任何基本类型,也可以是S...原创 2019-09-04 16:53:39 · 574 阅读 · 0 评论 -
algorithm头文件下的常用函数
max(),min(),abs()max(x,y)和min(x,y)的两个参数可以是浮点数,abs(x)的参数必须是整数,浮点型的绝对值要使用math头文件下的fabs。swap()reverse()reverse(it1, it2)可以将数组指针在[it1, it2)之间的元素或容器(可以是string)的迭代器在[it1, it2)范围内的元素进行反转。fill()与memset不...原创 2019-09-04 22:48:29 · 134 阅读 · 0 评论 -
priority_queue优先队列的用法
priority_queue的用法定义对元素的访问常用函数设置优先级以下代码来源:晴神《算法笔记》!!优先队列的队首元素是当前队列中优先级最高的。使用优先队列需添加头文件queue。定义priority_queue<typename> name;对元素的访问没有队列的front()和back()函数,只可以通过top()访问队首元素。常用函数push()top(...原创 2019-09-05 11:13:12 · 103 阅读 · 0 评论 -
一些常见数学问题的算法
最大公约数int gcd(int a, int b){ if(b == 0) return a; else return gcd(b, a%b);}最小公倍数lcm(a, b) = ab / d,ab有可能溢出,因此更恰当的写法是a/d*b。分数的四则运算分数的表示struct fraction{ int up, down;};分数的化简fraction reduct...原创 2019-09-04 23:32:02 · 276 阅读 · 0 评论 -
插入排序
基本思想:每一趟都将待排序部分的第一个元素插入到有序部分的合适位置。int a[maxn], n; //n为元素个数void selectsort(){ for(int i = 2; i <= n; i++){ int temp = a[i], j = i; while(j > 1 && temp < a[j-1]){ a[j] = a[j-...原创 2019-09-05 11:25:10 · 102 阅读 · 0 评论 -
选择排序
基本思想:每次从待排序部分选择最小的元素,与该部分的第一个元素交换。void selectsort(){ for(int i = 1; i <= n; i++){ int k = i; for(int j = i; j <= n; j++){ if(a[j] < a[k]){ k = j; } } int temp = a[i]; a[...原创 2019-09-05 11:19:13 · 86 阅读 · 0 评论 -
区间贪心
问题:给定n个开区间,选择尽可能多的区间,使得这些开区间两两之间没有交集。#include <cstdio>#include <algorithm>using namespace std;const int maxn = 110;struct inteval{ int x, y;}in[maxn];bool cmp(inteval a, inteval b)...原创 2019-09-05 11:56:17 · 57 阅读 · 0 评论 -
二分查找
问题:在有序数列中找到某数x。int binarysearch(int a[], int left, int right, int x){ int mid; while(left <= right){ //必选是<=,因为要判断x是否存在 mid = (left + right) / 2; if(a[mid] == x) return mid; els...原创 2019-09-05 12:09:04 · 112 阅读 · 0 评论 -
快速排序C++实现
//添加头文件stdlib.h和time.h, main()函数一开头加上srand((unsigned)time(NULL)) int randpartition(int a[], int left, int right){ int p = (int)round(1.0 * rand() / RAND_MAX * (right - left) + left); //四舍五入 swap(a[...原创 2019-09-05 14:52:33 · 173 阅读 · 0 评论 -
归并排序
递归实现const int maxn = 110;void merge(int a[], int l1, int r1, int l2, int r2){ int i = l1, j = l2; int temp[maxn], index = 0; while(i <= r1 && j <= r2){ if(a[i] <= a[j]){ tem...原创 2019-09-05 14:30:42 · 126 阅读 · 0 评论