
数据结构
文章平均质量分 82
DedicateToAI
这个作者很懒,什么都没留下…
展开
-
阿里3.25笔试题
自己写的bullshit,第一题5%,第二题0%第一题原来写的代码:思路是首先对矩阵所有数求和,然后取平均值,接下来,找到每一列最接近平均值的数,使得起伏最小。只通过了5%。#include <iostream>#include <cstdio>#include<vector>#include<algorithm>usi...原创 2020-04-09 23:01:18 · 731 阅读 · 0 评论 -
PAT甲级- 1040 Longest Symmetric String (25 分)
Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, givenIs PAT&TAP symmetric?, the longest symmetric sub-string iss PAT&TAP s, hence you...原创 2019-11-27 21:49:49 · 198 阅读 · 0 评论 -
PAT甲级 1007 Maximum Subsequence Sum (25 分)
Given a sequence ofKintegers {N1,N2, ...,NK}. A continuous subsequence is defined to be {Ni,Ni+1, ...,Nj} where1≤i≤j≤K. The Maximum Subsequence is the continuous subse...原创 2019-11-24 22:44:39 · 149 阅读 · 0 评论 -
字符串Hash
给出N个只有小写字母的字符串,求其中的不同的字符串的个数#include<iostream>#include<string>#include<algorithm>#include<vector>using namespace std;const int MOD = 1000000007;const int P = 1000001...原创 2019-11-22 23:28:40 · 136 阅读 · 0 评论 -
PAT甲级 - 1030 Travel Plan (30 分)
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path ...原创 2019-11-17 12:42:03 · 159 阅读 · 0 评论 -
PAT - 1034 Head of a Gang (30 分) 图的DFS
One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call betweenAandB, we say thatAandBis related. The weight of a relation is defined to be ...原创 2019-11-13 16:16:47 · 171 阅读 · 0 评论 -
PAT- 并查集-好朋友
并查集的重点是维护一个father数组,来表明各结点间的关系【好朋友】1. 问题描述:有一个叫做“数码世界”奇异空间,在数码世界里生活着许许多多的数码宝贝,其中有些数码宝贝之间可能是好朋友,并且数码宝贝世界有两条不成文的规定:第一,数码宝贝A和数码宝贝B是好朋友等价于数码宝贝B与数码宝贝A是好朋友第二,如果数码宝贝A和数码宝贝C是好朋友,而数码宝贝B和数码宝贝C也是好朋友,那...原创 2019-11-12 21:48:09 · 390 阅读 · 0 评论 -
PAT - A1043 Is It a Binary Search Tree (25 分)
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 less than the node's key. The right s...原创 2019-11-09 13:02:51 · 197 阅读 · 0 评论 -
1053 Path of Equal Weight (30 分)
Given a non-empty tree with rootR, and with weightWiassigned to each tree nodeTi. Theweight of a path fromRtoLis defined to be the sum of the weights of all the nodes along the path fr...原创 2019-11-08 13:53:34 · 164 阅读 · 0 评论 -
PAT - BFS
例题1,给出一个m*n的矩阵,矩阵中的元素为0或1,称位置x与其上下左右四个位置是相邻的。若有若干个1相邻,成这些1构成了一个块。例如如下的矩阵中输入样例: 6 7 0 1 1 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 0 1 1 1 0 1 0 0 1 1 1 1 0 0 0...原创 2019-11-05 16:21:45 · 192 阅读 · 0 评论 -
PAT - DFS
使用DFS可以解决枚举子序列问题:给定一个序列,枚举这个序列的所有子序列。因为每个数总有两种状态,选与不选,然后进行递归即可。枚举从N个整数中选择K个数的方案是子问题。例题:从序列A(n个数)中选K个数,使得和为x,且序列的平方和最大。 //有边界,提前判断//序列A中n个数选k个数使得和为x,最大平方和为maxSumSqu#include<cstdio>#incl...原创 2019-11-01 22:28:20 · 189 阅读 · 0 评论 -
一些比较难懂的递归题目 c++
全排列问题#include<cstdio>const int maxn = 11;int n, P[maxn], hashTable[maxn] = { false };void generateP(int index) { if (index == n + 1) { for (int i = 1; i <= n; i++) { printf("%d"...原创 2019-10-18 15:53:55 · 548 阅读 · 0 评论 -
排序 - PAT甲级 1025 PAT Ranking (25 分) C / C++
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;struct testee { char regis_num[15]; int score; int final_rank; int local_num; int local_rank;}tes...原创 2019-10-17 22:43:01 · 191 阅读 · 0 评论 -
LeetCode 9. Palindrome Number - C++
自己调试,写出来的一道题目诶,虽然是Easy难度的,但是也很开心了。注意:不要连续 if..if..if,容易导致连续满足条件,要用if...else if...else if...else if 或 if...else if...else。如果有溢出,直接返回0。步骤:1.如果是负数,直接返回false。2.如果是0或者个位数,直接返回true。3.其他情况,进...原创 2019-10-07 19:51:13 · 215 阅读 · 0 评论 -
LeetCode 8. String to Integer (atoi) c++
实现将字符串转换成整数的功能。1.str[i] - '0' 将对应位置的字符转换成整数2.str[i] >= '0' && str[i] <= '9' 判断是否整数3.进入每一个步骤首先判断是否越界4.完成该步判断后,i++保证循环继续进行具体步骤:1.首先跳过前面所有空格2.判断是否有符号的标识,有符号的标识就保存起来3.进入计数...原创 2019-10-07 16:53:05 · 145 阅读 · 0 评论 -
Leetcode.7. Reverse Integer
这道题是easy题,还是比较简单的,主要是需要考虑int的边界情况, -2^31 ~ 2^31 - 1。提前判断溢出,并返回0。解法1:按照Intuition写的,%操作获得末位数, /操作从后向前减小余数,但是 int数 正数比负数小1的问题没有解决,由10个案例没有通过,应该都是overflow的边界情况。开辟了新的vector数组,其实是没有必要的。class Solutio...原创 2019-09-29 21:05:43 · 139 阅读 · 0 评论 -
线性结构2 一元多项式的乘法与加法运算
02-线性结构2 一元多项式的乘法与加法运算(20 分)设计函数分别求两个一元多项式的乘积与和。输入格式:输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。输出格式:输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。...原创 2018-03-24 00:42:11 · 389 阅读 · 0 评论 -
树2 List Leaves
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.Input Specification:Each input file contains one test case. For each case, the first line gives a posi...原创 2018-03-30 21:47:03 · 216 阅读 · 3 评论 -
树1 树的同构
给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。图1图2现给定两棵树,请你判断它们是否是同构的。输入格式:输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤),即该树的结点数(此时假设结点从0到N−1编...原创 2018-03-29 10:27:10 · 248 阅读 · 0 评论 -
树3 Tree Traversals Again
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stac...原创 2018-03-31 23:47:26 · 308 阅读 · 0 评论 -
树4 是否同一棵二叉搜索树
给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果。于是对于输入的各种插入序列,你需要判断它们是否能生成一样的二叉搜索树。输入格式:输入包含若干组测试数据。每组数据的第1行给出两个正整数N (≤)和L,分别是每个序列插入元素的个数和需要检查的序列个数...转载 2018-04-12 10:04:28 · 176 阅读 · 4 评论 -
树5 Root of AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is...转载 2018-04-17 22:53:19 · 168 阅读 · 0 评论 -
树7 堆中的路径
将一系列给定数字插入一个初始为空的小顶堆H[]。随后对任意给定的下标i,打印从H[i]到根结点的路径。输入格式:每组测试第1行包含2个正整数N和M(≤),分别是插入元素的个数、以及需要打印的路径条数。下一行给出区间[-10000, 10000]内的N个要被插入一个初始为空的小顶堆的整数。最后一行给出M个下标。输出格式:对输入中给出的每个下标i,在一行中输出从H[i]到根结点的路径上的数据。数字间以...原创 2018-04-21 18:43:39 · 267 阅读 · 1 评论 -
06-图1 列出连通集
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N−1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。输入格式:输入第1行给出2个整数N(0)和E,分别是图的顶点数和边数。随后E行,每行给出一条边的两个端点。每行中的数字之间用1空格分隔。输出格式:按照"{ v1 v2 ... vk }"的格式,每行输出一...原创 2018-05-06 16:53:17 · 190 阅读 · 0 评论 -
03-树2 List Leaves
坚持!量变引起质变!03-树2 List Leaves (25 分)Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.Input Specification:Each input file contains one test cas...原创 2018-10-28 13:51:45 · 169 阅读 · 0 评论 -
03-树1 树的同构
给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。 图1图2现给定两棵树,请你判断它们是否是同构的。 输入格式:输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N...原创 2018-10-27 22:19:28 · 289 阅读 · 0 评论 -
LeetCode 5. Longest Palindromic Substring
求最长回文子串。解法1:直接暴力求解,两个函数,一个函数列出所有子串,一个函数判断是否回文串(遍历)。但是时间复杂度太高,O(n3),只能通过一半的测试用例。解法2:将字符串反转得到反转字符串rev,两个字符串相等,即为回文子串。按照这样的思路,主要还是两部分功能,一是列出所有子串,二是利用反转,判断是否回文串,时间复杂度O(n2)。测试用例:102/103class ...原创 2019-09-28 21:27:19 · 113 阅读 · 0 评论 -
LeetCode 6. ZigZag Conversion
思路1:自己想到的最直观的思路,观察每一行的数的规律,进行解决,但是自己写乱了,没有AC.思路2:首先构建一个Vector<string>数组,Vector中的每一个元素代表某一行的字符串。从左到右扫描原始字符串,然后按照顺序将字符插入对应行的字符串中。1.vector<string> rows(10); 初始生成一个容量为10的数组2.curRow,定义...原创 2019-09-29 12:26:27 · 139 阅读 · 0 评论 -
线性结构1 两个有序链表序列的合并
02-线性结构1 两个有序链表序列的合并(15 分)本题要求实现一个函数,将两个链表表示的递增整数序列合并为一个非递减的整数序列。函数接口定义:List Merge( List L1, List L2 );其中List结构定义如下:typedef struct Node *PtrToNode;struct Node { ElementType Data; /* 存储结点数据 */ ...原创 2018-03-23 13:36:42 · 394 阅读 · 2 评论