![](https://img-blog.csdnimg.cn/direct/37c02eb6a73e41e8bef07363b943caea.jpeg?x-oss-process=image/resize,m_fixed,h_224,w_224)
算法刷题
算法刷题
北枫凉
The greatest gift you can give yourself is that of education.
展开
-
百练4127 迷宫问题(BFS和DFS详解)
基础BFS搜索总时间限制: 1000ms 内存限制: 65536kB描述定义一个二维数组:int maze[5][5] = {0, 1, 0, 0, 0,0, 1, 0, 1, 0,0, 0, 0, 0, 0,0, 1, 1, 1, 0,0, 0, 0, 1, 0,};它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到...原创 2020-04-08 22:22:14 · 423 阅读 · 0 评论 -
百练4001 抓住那头牛
BFS问题总时间限制: 2000ms 内存限制: 65536kB描述农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000)。农夫有两种移动方式:1、从X移动到X-1或X+1,每次移动花费一分钟2、从X移动到2*X,每次移动花费一分钟假设牛没有意识到农夫的行动,站在原地不动。农夫...原创 2020-04-08 22:18:52 · 159 阅读 · 0 评论 -
C/C++建立二叉树时指针的指针,指针的引用,函数的返回值
在建立二叉树时,指针的指向发生变化时如何处理?1.使用Node root2.使用Node &root3.使用函数的返回值:Node insert(Node* root,int data)调用时root = insert(root,data);具体可参见二叉树统计单词...原创 2020-04-07 08:10:12 · 245 阅读 · 0 评论 -
C/C++语言实现二叉树统计单词
jiben yaoqiu原创 2020-04-06 00:01:05 · 561 阅读 · 0 评论 -
动态规划 入门2
数塔问题(动态规划的递推写法):数塔问题中也存在着大量的重复计算。利用递推从下往上计算,存在着状态转移方程dp[i][j] = max(dp[i+1][j],dp[i+1][i+1]) + f[i][j]代码实现#include<stdio.h>#include<algorithm>using namespace std;const int maxn ...原创 2020-03-31 23:18:32 · 79 阅读 · 0 评论 -
动态规划 最长回文串
思想状态:dp[i][j]:表示S[i]到S[j]是否是回文串,是为1,不是为0状态转移方程:dp[i][j] = dp[i+1][j-1] ,S[i]==S[j]dp[i][j] = 0, ,S[i]!=S[j]1.边界:dp[i][i] = 1,dp[i][i+1] = (S[i] == S[i+1]) ? 1:02.枚举:不能够使用连续枚举的方法(这样有的...原创 2020-04-02 16:22:26 · 86 阅读 · 0 评论 -
动态规划 最长连续子序列和
难点:主要是状态和状态转移方程的设置状态:dp[i]表示的是必须以A[i]结尾的连续序列的最大和状态转移方程:dp[i] = max(A[i],dp[i-1]+A[i])代码实现//北航机试2020/4/1#include<stdio.h>int max(int a,int b){ if(a>b) return a; else ret...原创 2020-04-01 20:47:06 · 161 阅读 · 0 评论 -
子函数中malloc的内存是否会被释放
如果子函数返回值是一个指针,在子函数中先用malloc定义了这个指针,然后return该指针。子函数需要return一个内部分配的空间,那么释放就归调用者来free了,否则内部free再返回已经是无效空间。...原创 2020-04-05 16:13:00 · 3710 阅读 · 2 评论 -
算法笔记 Chapter8迷宫问题
基础BFS搜索自做代码//2020/4/8#include<stdio.h>#include<queue>#include<string.h>using namespace std;struct Node{ int x,y; int steps; Node(int x_,int y_,int steps_):x(x_...原创 2020-04-08 22:32:12 · 130 阅读 · 0 评论 -
算法笔记 - Chapter8矩阵块个数
基础BFS自作答案//2020/4/8#include<stdio.h>#include<queue>#include<string.h>using namespace std;struct Node{ int x,y; Node(int x_,int y_):x(x_),y(y_){}};const int maxn...原创 2020-04-08 22:26:02 · 122 阅读 · 0 评论 -
数据结构 链表的基本操作
概念区分:头结点和头指针通常使用头指针来标识一个链表,如单链表L,头指针为NULL时表示一个空表。为了操作上的方便,在单链表第一个节点之前添加一个头结点。头结点的额指针域指向线性表的第一个元素节点。不管带不带头结点,头指针总是指向链表的第一个节点。头结点是带头结点链表的第一个节点。基本操作创建链表链表长度查找元素插入数据删除数据创建链表//2020/4/4#inclu...原创 2020-04-04 18:41:38 · 275 阅读 · 0 评论 -
printf()中的左对齐和右对齐
#include<stdio.h>int main(){ return 0; printf("%d%d\n",11111,22222);//默认左对齐,宽度是数据本身 printf("%-10d%d\n",11111,22222);//左对齐,宽度是10 printf("%10d%d",11111,22222);//右对齐,宽度是10}/*1111122222111...原创 2020-04-04 21:37:56 · 1749 阅读 · 0 评论 -
动态规划 最长公共子序列
难点:状态:dp[i][j]表示A左边的i个字符和B右边的j个字符形成的子串的最长公共子序列的长度状态转换方程:if(A[i-1] == B[j-1]) dp[i][j] = dp[i-1][j-1] + 1;else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);代码实现#include<stdio.h>#include<str...原创 2020-04-01 23:12:20 · 96 阅读 · 0 评论 -
动态规划 入门1
Fibonacci数列动态规划的写法:这是一种动态规划的递归实现(又称记忆化实现),解决大量的重复计算问题。#include<stdio.h>#include<algorithm>using namespace std;int dp[100];int Fib(int n){ if(n == 0||n==1) return 1;//递归边界 ...原创 2020-03-31 22:46:48 · 91 阅读 · 0 评论 -
sort函数中的cmp
bool cmp(T x,T y){ if(x > y) return true; else return false;}//true表示位置正确,无需进行交换//false表示位置错误,进行位置的交换原创 2020-04-04 21:01:49 · 347 阅读 · 0 评论 -
动态规划 最长不下降子序列
难点状态:以A[i]结尾的最长不下降子序列状态转移方程:dp[i] = max(1,dp[j]+1)代码实现#include<stdio.h>const int maxn = 1000;int A[maxn],dp[maxn];int main(){ int i,j; int n; scanf("%d",&n); for...原创 2020-04-01 21:19:29 · 219 阅读 · 0 评论 -
C语言中*和++的优先级
*和++为同一级别的运算符,只是结合顺序为由右向左。*w++ ;//右结合*(w++); ++*w ;//右结合++(*w);原创 2020-04-05 22:10:16 · 3866 阅读 · 0 评论 -
PAT A1052
1052 Linked List Sorting (25分)A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer ...原创 2020-03-27 23:07:27 · 94 阅读 · 0 评论 -
PAT A1032
1032 Sharing (25分)To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same ...原创 2020-03-27 22:07:52 · 131 阅读 · 0 评论 -
codeup 1918 简单计算器
简单计算器题目描述读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。输入测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。输出对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。样例输入 Copy30 / 90 - 26 + 97 -...原创 2020-03-27 18:52:31 · 189 阅读 · 0 评论 -
STL--queue队列的使用
队列常用的操作1.清空clear()2.大小size()3.判空empty()4.入队push()5.出队pop()—队首元素出队6.访问队首元素font()7.访问队尾元素back()//出队入队和访问元素注意:出队和访问元素(队首元素,队尾元素)的时候,都要判空STL中没有实现队列的清空clear()操作 //清空操作 while(!q.emp...原创 2020-03-27 15:37:33 · 309 阅读 · 0 评论 -
STL--stack栈的使用
栈的常用操作:1.清空clear()2.大小size()3.判空empty()4.入栈push()5.出栈pop()6.栈顶元素top()注意点:在使用pop()和top()函数的时候,需要先判断是否栈为空STL中没有清空clear()函数,需要自己编写:while(!s.empty())//判空 { s.pop();//出栈 }...原创 2020-03-27 15:10:21 · 812 阅读 · 0 评论 -
PAT A1087
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-03-25 15:46:34 · 138 阅读 · 0 评论 -
PAT A1072 最短路径
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-03-25 11:11:00 · 185 阅读 · 1 评论 -
PAT A1030
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 dec...原创 2020-03-24 22:53:39 · 186 阅读 · 0 评论 -
LeetCode 1 两数之和
两数之和给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。示例:给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1]方法1...原创 2020-03-24 09:05:38 · 67 阅读 · 0 评论 -
如何高效的使用力扣
编程能力就像任何其他技能一样,也是一个可以通过刻意练习大大提高的。大多数经典面试题目都有多种解决方案。 为了达到最佳的练习效果,题目练习两遍,如果可以的话,三遍会更好。在第二遍练习时,你可能会发现一些新的技巧或新的方法。 到第三遍的时候,你会发现你的代码要比第一次提交时更加简洁。 如果你达到了这样的效果,那么恭喜你,你已经掌握了正确的练习方法!记住:刻意练习并不意味着寻找答案并记住它,这种练...原创 2020-03-24 08:37:24 · 9163 阅读 · 0 评论 -
PAT刷题
PAT (Basic Level) Practice (中文)PAT (Advanced Level) PracticeA1009/*pat a1002time:2017/7/26 2019/7/26*/#include<cstdio>int main(){ double a[1000+ 10]= {0}; double b[1000+10] = {0}; ...原创 2019-07-26 21:12:57 · 500 阅读 · 0 评论