算法
黄色猴子
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
leetcode最小高度树
使用了BFS/拓扑排序class Solution {public: vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) { //只有一个根结点 if (n == 1) { return {0}; } //有两个结点,可以互为根结点 i原创 2021-02-04 17:38:02 · 134 阅读 · 0 评论 -
c++实现快速排序(优化版本)
#include<iostream>using namespace std;//交换两个元素void Swap_Elem(int *arr, int first, int second){ int temp = arr[first]; arr[first] = arr[second]; arr[first] = temp;}//将元素移动到正确位置int Patition(int *arr, int low, int high){ //三数取中,确保枢轴不是最小或者最原创 2020-12-23 17:47:20 · 319 阅读 · 0 评论 -
c++实现冒泡排序、快速排序、直接插入排序
#include<iostream>#include<vector>using namespace std;//记录类template<class key_type>class Record{private: key_type key; //键值public: Record(){}//构造函数 void set_key(int k) { this->key = k; } //设置键值 key_type get_key() { return原创 2020-12-23 16:32:32 · 521 阅读 · 0 评论 -
c++实现哈希查找(散列表)
使用除留余数法构造哈希函数使用线性探测法来解决冲突#include<iostream>using namespace std;const int MAX = 65535; //设置一个不可能输入的值 //建立哈希表void Creat_HashTable(int *arr, int num){ int x = 0, key = 0; // x 为将要输入的值, key 为键值下标 //初始化哈希表为MAX for (int i = 0; i < num; i++)原创 2020-12-16 17:03:34 · 751 阅读 · 0 评论 -
c++实现二分查找
#include<iostream>using namespace std;void test(int *arr, int low, int high, int key){ int mid = 0; while (high >= low) { mid = (high + low) / 2; if (arr[mid] == key) { cout << "找到了数字" << arr[mid] << endl; ret原创 2020-12-16 16:55:59 · 1072 阅读 · 0 评论 -
c++实现顺序查找(优化版本)
using namespace std;void sequence_search(int *arr, int num, int key){ while (arr[num] != key) { num--; } if (arr[num] == key && num != 0) { cout << "找到了数字" << arr[num] << endl; } else { cout << "没有找到!" <&原创 2020-12-16 16:53:49 · 257 阅读 · 0 评论 -
c++实现Dijkstra算法(最短路径)
#include<iostream>#include<string>using namespace std;const int MAX = 10; //限定最大的顶点数const int _INFINITY = 65535; //定义一个不可能输入的数字class Graph{private: int vertex_num; //顶点数 int edge_num; //边数 int weight; //权值 string vertex[MAX]; //顶点数组原创 2020-12-05 21:03:04 · 2381 阅读 · 0 评论 -
c++实现kruskal算法(最小生成树)
#include<iostream>using namespace std;const int MAX = 15; //数组最大容量struct Edge{ int begin; int end; int weight;};class Graph{private: int vertex_num; //顶点数 int edge_num; //边数 Edge edges[MAX]; //边集数组 char vertex[MAX]; //顶点数组 int pa原创 2020-11-30 23:29:55 · 741 阅读 · 0 评论 -
c++实现prim算法(最小生成树)
#include<iostream>using namespace std;const int MAX = 10;const int _INFINITY = 65535; //初始化最小值为一个不可能的数值class Graph{private: int vertex_num; //顶点数 int edge_num; //边数 int weight; //权值 char vertex[MAX]; //顶点数组 int edge[MAX][MAX]; //邻接矩阵 in原创 2020-11-29 15:44:39 · 3027 阅读 · 0 评论 -
c++实现哈夫曼编码
#include<iostream>#include<string>using namespace std;//哈夫曼树结构typedef struct HFnode{ int data; //字母权值 string leter; //对应字母 HFnode *lson, *rson; //左右子树 HFnode *next, *parent; //森林域和双亲域}*HFptr;//哈夫曼所有结点的字符集合//用于后面的译码typedef struct原创 2020-11-26 22:30:53 · 1149 阅读 · 0 评论 -
c++实现八皇后问题(详细注释)
#include<iostream>#include<string>using namespace std;class Queen{private: int Place(int k); //判断皇后k是否冲突 int *x; //皇后的位置 int num; //皇后的个数public: Queen(int n); //构造函数 ~Queen(); //析构函数 void Set_Queen(); //放入皇后 void Print_Queen(); /原创 2020-10-25 00:46:58 · 1275 阅读 · 0 评论 -
c++实现发纸牌算法
#include<iostream>#include<ctime>#include<string>using namespace std;string str1[4] = { "黑桃", "红桃", "方块","梅花" };string str2[13] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };int sign[4][13] = { 0 };class原创 2020-10-24 23:04:37 · 1183 阅读 · 0 评论 -
c++实现顺序表
#pragma once#include<string>#include<iostream>using namespace std;class SeqList{public: SeqList(int sz, int leng); //构造函数 void mune(); //菜单 void resize(int newSize); //扩容 int length() { return len; }//表长度 void input(); //输入线性表中原创 2020-10-17 00:14:36 · 948 阅读 · 0 评论 -
c++实现并集(A∪B = C)
#include<iostream>using namespace std;class int_set{private: int max_size; //最大容量 int current_size; //当前大小 int *p; //动态数组public: //构造函数 int_set(int &arr_size) :max_size(arr_size), current_size(0) { this->p = new原创 2020-10-17 00:11:15 · 3256 阅读 · 0 评论 -
c++实现约瑟夫环
#include<iostream>using namespace std;typedef struct Node{ int data; Node *next;};class JosephRing{private: Node *rear;//尾结点public: JosephRing(int n);//构造函数,初始化n个结点的循环链表 ~JosephRing();//析构函数 void Joseph(int m);//密码为m,打印出环顺序};Josep原创 2020-10-17 00:08:05 · 2389 阅读 · 0 评论 -
c++实现表达式求值
#include<iostream>using namespace std;#include<string>class Expression{private: string str; //表达式 int Compare(char str1, char str2); //比较 str1 和 str2 的优先级public: Expression(string str3); //构造函数 ~Expression(); //析构函数 double& C原创 2020-10-17 00:03:58 · 2217 阅读 · 1 评论 -
c++实现kmp算法
void initNextArray(string p){ int k = -1; int j = 0; next[0] = -1; while(j < p.size()-1){ if(k == -1 || p[k] == p[j]) next[++j] = ++k; else k = next[k]; }}原创 2020-10-16 00:28:39 · 452 阅读 · 0 评论 -
c++实现数组元素循环左移
#include<iostream>using namespace std;void move_left(int *arr, int arr_num, int move_num){ move_num = move_num % arr_num; while (move_num--) { int index = arr[0]; for (int i = 0; i < arr_num - 1; i++) { arr[i] = arr[i + 1]; }原创 2020-09-12 10:19:37 · 4551 阅读 · 0 评论
分享