![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法与结构
c++ 算法练习专栏
Cooler Than Me
这个作者很懒,什么都没留下…
展开
-
c++判断搜索二叉树、完全二叉树
搜索二叉树:中序遍历为升序任意一个节点的值一定大于该节点左子树中的任意一个节点的值,同时满足该节点的值小于其右子树中的任意一个节点的值(解决效率问题)方法:非递归版本二叉树中序遍历,把打印过程改成判断。#include<iostream>#include<stack>#include<limits.h>using namespace std;...原创 2020-03-04 16:55:27 · 548 阅读 · 0 评论 -
c++二叉树的后继节点
#include <iostream>using namespace std;struct Node{ int value; Node* left; Node* right; Node* parent; Node(int valueue): value(value),left(NULL),right(NULL),paren...原创 2020-03-04 00:55:40 · 163 阅读 · 0 评论 -
c++ 二叉树前序、中序、后续 递归和非递归版本
#include<iostream>#include<stack>using namespace std;struct node{ int value; node* left; node* right; node(int value): value(value), left(NULL),right(NULL) {}...原创 2020-03-03 21:42:37 · 89 阅读 · 0 评论 -
c++ 打印两个链表得公共部分
题目:给定两个有序链表的头指针 head1 和 head2,打印两个链表的公共部分。左程云 代码 c++ 实现:#include <iostream>using namespace std;struct node{ int value; node *next;};void add_node(node **head, int value) { ...原创 2020-02-26 00:55:53 · 447 阅读 · 0 评论 -
c++链表得创建、插入、删除
#include <iostream>using namespace std;typedef struct node{ int value; node* next;}student;student* creatList(int n){ //创建链表 student* head = new student(); student* pre ...原创 2020-02-25 00:22:33 · 77 阅读 · 0 评论 -
之字形打印一个矩阵 c++
之字形打印一个矩阵1 2 34 5 67 8 9打印结果是 1 2 4 7 5 3 6 8 9。要求额外空间复杂度是O(1)#include <iostream>#include <vector>using namespace std;void printMatrixZigZag (vector<vector<in...原创 2020-02-22 17:02:28 · 293 阅读 · 0 评论 -
c++顺时针打印矩阵
题目描述输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.#include <iostream>using namespace std;void PrintE...原创 2020-02-21 20:10:01 · 318 阅读 · 0 评论 -
C++两个队列实现一个栈,两个栈实现一个队列
左程云算法课程:两个队列实现一个栈:#include <iostream>#include <queue>using namespace std;template <class T>class twoQueue{private: queue <T> data; queue <T> help;pub...原创 2020-02-21 13:26:20 · 132 阅读 · 0 评论 -
返回栈中最小的元素,要求时间复杂度为O(1) c++
标题实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。【要求】1.pop、push、getMin操作的时间复杂度都是O(1)。2.设计的栈类型可以使用现成的栈结构。#include <iostream>#include <stack>using namespace std;template <class T>cla...原创 2020-02-19 19:02:54 · 159 阅读 · 0 评论 -
求相邻两数的最大差值 c++
题目:给定一个数组,求如果排序之后,相邻两数的最大差值,要求时间复杂度O(N),且要求不能用非基于比较的排序。思路:因为不能使用非基于比较的排序,所以不能应用桶排序后遍历找最大差值。但可以应用桶排序的思想:创建一个比原数组个数加一的桶,数组的最小值放在第0号桶里,数组的最大值放在最后一个桶里,然后遍历数组,桶内只放该区域的最大值和最小值。遍历之后中间一定会有一个空桶,要注意的是空桶的左边的最小值...原创 2020-02-18 17:23:51 · 1452 阅读 · 0 评论 -
堆排序C++
左神堆排序 C++#include <iostream>#include <math.h>using namespace std;void Swap(int arr[], int i, int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;}void heapInsert(...原创 2020-02-17 17:42:25 · 55 阅读 · 0 评论 -
快速排序 c++
左神课程快速排序 c++版本#include <iostream>using namespace std;void Swap(int arr[], int i, int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;}int *partition(int array[], int L, ...原创 2020-02-17 12:14:24 · 89 阅读 · 0 评论 -
荷兰国旗系列问题
问题:给定一个数组arr和一个数字num,请把小于num的数放在数组的左边,等于num的数放在数组的中间,大于num的数放在数组的右边。要求额外空间复杂度O(1),时间复杂度O(N)#include <iostream>using namespace std;void Swap(int arr[], int i, int j){ int temp = arr[i];...原创 2020-02-16 16:49:28 · 107 阅读 · 0 评论 -
归并排序--小和问题
问题描述在一个数组中, 每一个数左边比当前数小的数累加起来, 叫做这个数组的小和。 求一个数组的小和。样例[1,3,4,2,5]1左边比1小的数, 没有;3左边比3小的数, 1;4左边比4小的数, 1、 3;2左边比2小的数, 1;5左边比5小的数, 1、 3、 4、 2;所以小和为1+1+3+1+1+3+4+2=16思路:归并排序#include <iostream&...原创 2020-02-16 15:03:15 · 133 阅读 · 0 评论 -
和为S的连续数列
输入一个正数target,打印出所有和为target的连续正数序列(至少含有两个数)。例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以结果打印出3个连续序列1~5、4~6和7~8>#include <iostream>using namespace std;void PrintNumber(int small, int big){ for (...原创 2020-02-15 19:58:22 · 225 阅读 · 0 评论 -
和为S的两个数
题目:输入一个递增排序的数组nums和一个数字target,在数组中查找两个数,使得它们的和正好是target。如果有多对数字的和等于target,输出全部组合。#include <iostream>using namespace std;void find(int nums[],int n,int target){ int i = 0; int j = n-1...原创 2020-02-15 18:00:02 · 142 阅读 · 0 评论