- 博客(30)
- 资源 (1)
- 收藏
- 关注
原创 计算时间 c++
文章目录时间戳与计算程序运行时间 时间戳与计算程序运行时间 #include <chrono> #include <iostream> #include <random> using namespace std; inline float get_random_float(float min, float max) { std::random_device dev; std::mt19937 rng(dev()); std::uniform_real_d
2021-08-29 18:30:56
250
原创 随机数生成
文章目录c++随机数生成 c++随机数生成 可以直接运行 #include <random> #include <iostream> using namespace std; inline float get_random_float(float min, float max) { std::random_device dev; std::mt19937 rng(dev()); std::uniform_real_distribution<float> di
2021-08-29 17:47:18
260
原创 单词的压缩编码
leetcode820题,单词的压缩 给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A。 例如,如果这个列表是 [“time”, “me”, “bell”],我们就可以将其表示为 S = “time#bell#” 和 indexes = [0, 2, 5]。 对于每一个索引,我们可以通过从字符串 S 中索引的位置开始读取字符串,直到 “#” 结束,来恢复我们之前的单词列...
2020-03-28 18:07:32
474
原创 二进制、十进制与十六进制之间互相转换
using namespace std; //二进制转换为十进制 int two_to_ten(string str) { int res = 0, length = str.size(); for (int i = 0; i < length; i++) { res += (str[i] - '0') * pow(2, length-i-1); } return res; ...
2019-12-11 15:55:24
428
原创 轮流取数
在一个数组上,每次只能去左边界或右边界,取完数以后在原数组删除所取的数。现在有两个人轮流取,先手取所得到的的数和最大,还是后手取所得数和大。
2019-07-15 16:01:21
965
原创 判断一棵树是否为平衡二叉树
平衡二叉树即为树中任意节点的左右两子树高度差不超过1 struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int a) :val(a), left(NULL), right(NULL) {}; }; struct rType { bool isBalanced; int height; rType...
2019-06-29 14:02:41
240
原创 判断是否为完全二叉树
首先完全二叉树即为一颗二叉树直到空节点前一直都是连续的树 #include <iostream> #include <stack> #include <queue> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int a...
2019-06-28 16:22:35
221
原创 判断一棵树是否为搜索二叉树
#include <iostream> #include <stack> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int a) :val(a), left(NULL), right(NULL) {}; }; void m...
2019-06-28 15:17:58
357
原创 二叉树的宽度优先遍历
#include <iostream> #include <queue> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int a) :val(a), left(NULL), right(NULL) {}; }; 单纯的宽度优先 v...
2019-06-28 10:30:50
278
原创 二叉树的后续遍历的递归与非递归方法实现
#include <iostream> #include <stack> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int a) :val(a), left(NULL), right(NULL) {}; }; 后序遍历的递归方法...
2019-06-28 09:57:14
230
原创 二叉树的中序遍历递归与非递归方法实现
#include <iostream> #include <stack> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int a) :val(a), left(NULL), right(NULL) {}; }; 中序遍历的递归方法...
2019-06-28 09:54:46
221
原创 二叉树的前序遍历递归与非递归方法的实现
二叉树结构 #include <iostream> #include <stack> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int a) :val(a), left(NULL), right(NULL) {}; }; 递...
2019-06-28 09:51:04
210
原创 对于一个由0..n的所有数按升序组成的序列,我们要进行一些筛选,每次我们取当前所有数字中从小到大的第奇数位个的数,并将其丢弃。重复这一过程直到最后剩下一个数。请求出最后剩下的数字。
输入描述: 每组数据一行一个数字,为题目中的n(n小于等于1000)。 输出描述: 一行输出最后剩下的数字。 示例1 输入 500 输出 255 分析 #include<iostream> #include<cmath> using namespace std; int main() { int n; while (cin >> n) { int k...
2019-06-14 15:55:31
1468
1
原创 已知两数和,求两数位置
Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where...
2019-06-14 15:06:31
161
原创 回文链表
对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。 给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。 测试样例: 1->2->2->1 返回:true /* struct ListNode { int val; struct ListNode *next; ...
2019-06-13 22:34:58
86
原创 划分链表
小于于divot的放前边,等于divot的放中间,大于divot的放后边 //划分链表,小于pivot的放前面,等于pivot的放中间,大于pivot的放后边 TreeList* partition_list(TreeList*p, int pivot) { TreeList* sH = NULL; TreeList*sT = NULL; TreeList *eH = NULL; Tree...
2019-06-13 14:43:19
430
2
原创 判断是不是回文链表的方法_3种
int main() { TreeList* p = new TreeList(8); p->next = new TreeList(9); p->next->next = new TreeList(8); //p->next->next->next = new TreeList(8); TreeList* fast = p; TreeList* ...
2019-06-12 21:56:06
455
原创 stl::map的一些常用操作
map<char, int>a;//先声明一个对象 /*添加数据*/ a.insert({ 'a',1 }); a.insert(pair<char, int>('b', 2)); a['c'] = 4; auto re=a.insert(make_pair('a', 8)); //这一个也是加入数据的方式,但是如果已经有了,就不会加入 cout <&l...
2019-06-11 20:31:27
309
原创 c++中各类型占用字节大小
今天突然想不起来c++64位机上各个基础类型占用的字节空间,用程序输出了一下,记下来: int main() { int a; char b; float c; double d; string e; bool f; cout << "int :" << sizeof(a) << endl; cout << "char :" &l...
2019-06-11 17:13:44
538
原创 快速排序
快速排序的时间复杂度为O(NlogN),空间复杂度为O(1),不具有稳定性,相比于其他时间复杂度为O(nlogn)的排序算法,快速排序的常量更好,实际会更快。 由于今天比较累,就不画图来详细解释了,可以看看其他人的,大家画的都挺好 在代码中用到了随机数,这是因为随机快速排序的时间复杂度才是我们所说的O(n*logn),如果不是随机取基准的话,按照最坏情况估计,时间复杂度为O(N^2)。 /*快速排...
2019-06-10 19:30:41
143
原创 堆排序
看代码的时候先看heap_sort,再看adjustTree() /*堆排序,利用一个完全二叉树来实现,一般有自带的priority_queue<T>来实现,但是还是写一下 (大顶堆) 时间复杂度为O(N*logN),空间复杂度为O(1),但是不稳定*/ //堆排序中最核心的代码就是调整指定位置元素在二叉树中的位置,所以先写它 template<t...
2019-06-10 17:14:51
156
原创 归并排序
归并排序的思路为将区间划分为两块,将两部分都排好序后再合并在一起。思路图如下: //归并排序 [L,R]区间上进行归并排序 归并排序用到了递归 //时间复杂度是O(N*logN),空间复杂度是O(N),保持稳定性 template<typename T> void mer(vector<T>&vec, int L, int mid, int R) { int ...
2019-06-10 10:33:09
135
原创 插入排序
插入排序 插入排序的时间复杂度是O(N^2),空间复杂度是O(1),因为在程序实现过程中只会申请有限几个变量。 下面是实现代码 //插入排序 [l,r)区间上进行插入排序 /*数据量较小的时候O(n^2)与O(n*logn)区别不大,而且常数项可能更好一点 template<typename T> void insertion_sort(vector<T>&...
2019-06-10 09:49:40
171
数据分析软件.rar
2019-06-12
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅