
数据结构
豌豆射手GCC
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
递归+二叉树的前中后序遍历转换 105. 从前序与中序遍历序列构造二叉树 106. 从中序与后序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树根据一棵树的前序遍历与中序遍历构造二叉树。注意:你可以假设树中没有重复的元素。例如,给出前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7]返回如下的二叉树: 3 / \ 9 20 / \ 15 7解题递归得到每个子节点,返回节点;注意点右子树的节点为getnode(preorder,inorder,i+t-start+1,t+1,end);原创 2020-05-17 14:12:21 · 198 阅读 · 0 评论 -
迭代+莫里斯 94. 二叉树的中序遍历
94. 二叉树的中序遍历给定一个二叉树,返回它的中序 遍历。示例:输入: [1,null,2,3] 1 \ 2 / 3输出: [1,3,2]进阶: 递归算法很简单,你可以通过迭代算法完成吗?迭代算法:堆栈利用堆栈,根节点入栈——进入左子节点;若当前节点为NULL,栈内节点出栈并放入结果数组——进入右子节点;直到堆栈为空停止;(栈内需要新放入一个空节点,不然会提前退出);/** * Definition for a binary tree nod原创 2020-05-15 14:21:48 · 165 阅读 · 0 评论 -
最大子数列和+动态处理 1007 Maximum Subsequence Sum (25分)
1007 Maximum Subsequence Sum (25分)Given a sequence of K integers { N1 , N2 , …, NK }. A continuous subsequence is defined to be { Ni , Ni+1 , …, Nj } where 1≤i≤j≤K. Th...原创 2020-04-23 14:07:46 · 252 阅读 · 0 评论 -
多叉树层序遍历+统计多叉树叶节点 1004 Counting Leaves (30分)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.Input Specification:Each input file contains one test case. Each case starts w...原创 2020-04-22 14:04:40 · 1177 阅读 · 0 评论 -
链表表示二叉树+二叉搜索树+AVL树 复习
二叉树的结点表示左子树,右子树,结点数据typedef struct TNode *Position;typedef Position BinTree; /* 二叉树类型 */struct TNode{ /* 树结点定义 */ ElementType Data; /* 结点数据 */ BinTree Left; /* 指向左子树 */ BinTree Rig...原创 2020-04-22 13:03:36 · 185 阅读 · 0 评论 -
树结构表示集合+并查集
集合运算:交、并、补、差,判定一个元素是否属于某一集合采用数组方式储存集合;#define MAXN 1000 /* 集合最大元素个数 */typedef int ElementType; /* 默认元素可以用非负整数表示 */typedef int SetName; /* 默认用根结点的下标作为集合名称 *...原创 2020-04-22 12:26:31 · 1197 阅读 · 0 评论 -
快速排序+动态规划+输出细节 1101 Quick Sort (25分)
1101 Quick Sort (25分)There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the piv...原创 2020-04-21 12:44:56 · 635 阅读 · 0 评论 -
链表结构+vector+reverse函数 1074 Reversing Linked List (25分)
1074 Reversing Linked List (25分)Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you...原创 2020-04-20 23:40:04 · 203 阅读 · 0 评论 -
散列查找+二次探测法 1078 Hashing (25分)
1078 Hashing (25分)The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to...原创 2020-04-20 12:13:01 · 350 阅读 · 0 评论 -
表排序+环的分类 1067 Sort with Swap(0, i) (25分)
1067 Sort with Swap(0, i) (25分)Given any permutation of the numbers {0, 1, 2,…, N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use?...原创 2020-04-19 12:40:13 · 215 阅读 · 0 评论 -
结构数组排序+compare函数+多细节 1075 PAT Judge (25分)
1075 PAT Judge (25分)The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT.Input Specification:...原创 2020-04-18 15:18:43 · 279 阅读 · 0 评论 -
快速排序 09-排序1 排序 (25分)
快速排序取一个数,利用双指针,找到该数的位置,再递归处理该数左边的数组和右边的数组;算法的速度关键在于每次找的的数最好都为数组中的中位数;方法1:每次都取头部元素,找到其位置;#include<iostream>using namespace std;#define MAXN 100010int N;int T[MAXN]; void input(){ ci...原创 2020-04-17 13:31:10 · 263 阅读 · 0 评论 -
插入排序+堆排序 09-排序3 Insertion or Heap Sort (25分)
09-排序3 Insertion or Heap Sort (25分)According to Wikipedia:Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort remov...原创 2020-04-16 13:22:33 · 242 阅读 · 0 评论 -
插入排序+非递归归并排序 09-排序2 Insert or Merge (25分)
09-排序2 Insert or Merge (25分)According to Wikipedia:Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one ...原创 2020-04-16 13:14:29 · 241 阅读 · 0 评论 -
排序算法 冒泡+插入+希尔+选择+堆排序+归并+快排
冒泡排序特点:稳定排序优点:可用于链表排序,数组排序复杂度:顺序最好O(N),逆序最坏O(N^2)插入排序特点:稳定排序复杂度:顺序最好O(N),逆序最坏O(N^2)void InsertionSort( ElementType A[], int N ){ /* 插入排序 */ int P, i; ElementType Tmp; for...原创 2020-04-15 14:49:37 · 182 阅读 · 0 评论 -
AOV AOE 拓扑排序算法 复习
AOV(Activity on Edge)顶点为任务,按照任务先后顺序前后排序;用于排序较好;排序结果不唯一/* 邻接表存储 - 拓扑排序算法 */ bool TopSort( LGraph Graph, Vertex TopOrder[] ){ /* 对Graph进行拓扑排序, TopOrder[]顺序存储排序后的顶点下标 */ int Indegree[MaxVert...原创 2020-04-15 00:21:25 · 551 阅读 · 0 评论 -
AOE 拓扑排序+求最短工期+求各边的机动时间+得到关键路径 08-图9 关键活动 (30分)
08-图9 关键活动 (30分)假定一个工程项目由一组子任务构成,子任务之间有的可以并行执行,有的必须在完成了其它一些子任务后才能执行。“任务调度”包括一组子任务、以及每个子任务可以执行所依赖的子任务集。比如完成一个专业的所有课程学习和毕业设计可以看成一个本科生要完成的一项工程,各门课程可以看成是子任务。有些课程可以同时开设,比如英语和C程序设计,它们没有必须先修哪门的约束;有些课程则不可以同...原创 2020-04-15 00:11:20 · 3457 阅读 · 0 评论 -
C 最小生成树 + Prim算法 08-图7 公路村村通 (30分)
08-图7 公路村村通 (30分)现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本。输入格式:输入数据包括城镇数目正整数N(≤1000)和候选道路数目M(≤3N);随后的M行对应M条道路,每行给出3个正整数,分别是该条道路直接连通的两个城镇的编号以及该道路改建的预算成本。为简单起见,城镇从1到N编号。输出格式:输出村村通...原创 2020-04-14 14:51:39 · 465 阅读 · 0 评论 -
C 拓扑排序+求总工期时长 08-图8 How Long Does It Take (25分)
08-图8 How Long Does It Take (25分)Given the relations of all the activities of a project, you are supposed to find the earliest completion time of the project.Input Specification:Each input file con...原创 2020-04-14 14:41:09 · 308 阅读 · 0 评论 -
最小生成树 Prim算法 Kruskal算法
最小生成树的特征树的特征:1.无回路2.V个顶点有V-1条边生成树的特征:1.包含树的所有顶点2.V-1条边都在图里最小:边的权重和最小Prim算法每次找到树外面的边权值最小的结点;将该结点加入树内;关键点找结点时需要同时满足1.只能用图里已有的边连接结点2.只能正好用掉V-1条边3.加入新的结点后不能有回路出现解决办法如何判断结点是否收录进树中:收录后的结点与树...原创 2020-04-14 14:20:32 · 210 阅读 · 0 评论 -
1018 DFS求最短路径+记录路径+路径结点权值的比较 Public Bike Management (30分)
1018 Public Bike Management (30分)There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and retur...原创 2020-04-13 21:16:18 · 530 阅读 · 0 评论 -
哈夫曼树 与 哈夫曼编码 (由最小堆构建)复习
哈夫曼树每个结点的权值乘以与根结点的路径长度之和最小的树——最优二叉树哈夫曼树的特点: 没有度为1的结点; 哈夫曼树的任意非叶节点的左右子树交换后仍是哈夫曼树; n个叶子结点的哈夫曼树共有2n-1个结点; 对同一组权值{w1 ,w2, …… , wn},存在不同构的哈夫曼树哈夫曼树的构成将二叉树按结点权值大小转化成最小堆,每次取堆定两个元素组成新的二叉树结点,插回堆中;当...原创 2020-04-13 01:02:28 · 624 阅读 · 0 评论 -
C DFS+最短路径+邻接表表示+地铁换乘(map保存地铁线路) 1131 Subway Map (30分)
1131 Subway Map (30分)In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now you are supposed t...原创 2020-04-12 22:49:56 · 2474 阅读 · 0 评论 -
C Dijkstra+结点权值+边权值+统计边数+统计最短路径个数+保存路径 1087 All Roads Lead to Rome (30分)
1087 All Roads Lead to Rome (30分)Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happi...原创 2020-04-12 13:25:45 · 373 阅读 · 0 评论 -
C 堆 通用构建方法 复习
堆1.堆结点的类型定义堆的当前元素个数堆的最大容量存储元素的数组typedef struct HNode *Heap; /* 堆的类型定义 */struct HNode { ElementType *Data; /* 存储元素的数组 */ int Size; /* 堆中当前元素个数 */ int Capacity; /* 堆的最大容量...原创 2020-04-11 23:16:43 · 127 阅读 · 0 评论 -
C Dijkstra算法 多次使用 1072 Gas Station (30分)
1072 Gas Station (30分)A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must gua...原创 2020-04-11 22:26:17 · 212 阅读 · 0 评论 -
C Dijkstra拓展 含结点权重 1003 Emergency (25分)
1003 Emergency (25分)As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams i...原创 2020-04-11 14:59:58 · 169 阅读 · 0 评论 -
C 图的链表表示 和 邻接矩阵表示
图的链表表示单源无权最短路径——BFS可以考虑用链表表示——更节省内存空间;#define MaxVertexNum 100 /* 最大顶点数设为100 */typedef int Vertex; /* 用顶点下标表示顶点,为整型 */typedef int WeightType; /* 边的权值设为整型 */typedef char DataTyp...原创 2020-04-10 23:45:11 · 370 阅读 · 0 评论 -
C Dijkstra 算法 floyd算法 拓展 07-图6 旅游规划 (25分)
07-图6 旅游规划 (25分)有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。输入格式:输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0~(N−1);M是高速...原创 2020-04-10 21:38:48 · 1111 阅读 · 0 评论 -
C BFS+无权最短路径记录 07-图5 Saving James Bond - Hard Version (30分)
07-图5 Saving James Bond - Hard Version (30分)This time let us consider the situation in the movie “Live and Let Die” in which James Bond, the world’s most famous spy, was captured by a group of drug d...原创 2020-04-10 15:33:51 · 184 阅读 · 0 评论 -
C Floyd算法 邻接矩阵表示图 07-图4 哈利·波特的考试 (25分)
07-图4 哈利·波特的考试 (25分)哈利·波特要考试了,他需要你的帮助。这门课学的是用魔咒将一种动物变成另一种动物的本事。例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等。反方向变化的魔咒就是简单地将原来的魔咒倒过来念,例如ahah可以将老鼠变成猫。另外,如果想把猫变成鱼,可以通过念一个直接魔咒lalala,也可以将猫变老鼠、老鼠变鱼的魔咒连起来念:hahahehe。现在...原创 2020-04-09 21:44:51 · 381 阅读 · 0 评论 -
C 带有计数的BFS 数组存储图 1076 Forwards on Weibo (30分)
1076 Forwards on Weibo (30分)Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with...原创 2020-04-09 13:34:41 · 128 阅读 · 1 评论 -
C 记录层数的BFS 06-图3 六度空间 (30分)
06-图3 六度空间 (30分)“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图1所示。图1 六度空间示意图“六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社...原创 2020-04-09 12:26:09 · 543 阅读 · 0 评论 -
C 06-图2 Saving James Bond - Easy Version (25分)
This time let us consider the situation in the movie “Live and Let Die” in which James Bond, the world’s most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land ...原创 2020-04-09 01:23:47 · 237 阅读 · 0 评论 -
C 图的构建 简单的BFS和DFS 06-图1 列出连通集 (25分)
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N−1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。输入格式:输入第1行给出2个整数N(0<N≤10)和E,分别是图的顶点数和边数。随后E行,每行给出一条边的两个端点。每行中的数字之间用1空格分隔。输出格式:按照格式,每行输出一个连通集。先输出DFS的结果,再...原创 2020-04-09 01:16:31 · 1045 阅读 · 0 评论 -
C 堆栈性质的运用 02-线性结构4 Pop Sequence (25分)
02-线性结构4 Pop Sequence (25分)Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, …, N and pop randomly. You are supposed to tell if a given sequence of numbers is a ...原创 2020-04-07 21:35:17 · 255 阅读 · 0 评论 -
C 最小堆的构建 霍夫曼树的性质 05-树9 Huffman Codes (30分)
05-树9 Huffman Codes (30分)In 1953, David A. Huffman published his paper “A Method for the Construction of Minimum-Redundancy Codes”, and hence printed his name in the history of computer science. As a...原创 2020-04-07 16:10:06 · 273 阅读 · 0 评论 -
C 树表示集合 05-树8 File Transfer (25分) 按秩归并+路径压缩
05-树8 File Transfer (25分)We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send...原创 2020-04-07 00:21:10 · 272 阅读 · 0 评论 -
C 最小堆的构建与路径查找 05-树7 堆中的路径 (25分)
05-树7 堆中的路径 (25分)将一系列给定数字插入一个初始为空的小顶堆H[]。随后对任意给定的下标i,打印从H[i]到根结点的路径。输入格式:每组测试第1行包含2个正整数N和M(≤1000),分别是插入元素的个数、以及需要打印的路径条数。下一行给出区间[-10000, 10000]内的N个要被插入一个初始为空的小顶堆的整数。最后一行给出M个下标。输出格式:对输入中给出的每个下标i,在...原创 2020-04-06 14:49:58 · 229 阅读 · 0 评论 -
C 完全二叉树和二叉搜索树 列表表示树 04-树6 Complete Binary Search Tree (30分)
04-树6 Complete Binary Search Tree (30分)A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys ...原创 2020-04-06 01:10:42 · 189 阅读 · 0 评论