![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
文章平均质量分 69
_天明_
这个作者很懒,什么都没留下…
展开
-
最长重复子数组&&最长公共子序列
最长重复子数组01143. 最长公共子序列给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。例如,“ace” 是 “abcde” 的子序列,但 “aec” 不是 “abcde” 的子序列。两个字符串的「公共子序列」是这两个字符串所共同拥有的子序列。若这两个字符串没有公共子序列,则返回 0。...原创 2020-08-17 19:44:50 · 276 阅读 · 0 评论 -
字符串匹配——Sunday算法
https://blog.csdn.net/q547550831/article/details/51860017链接class Solution {public: int strStr(string haystack, string needle) { int n=haystack.size(); int m=needle.size(); unordered_map<char,int> dd; for(int i转载 2020-06-18 16:23:36 · 160 阅读 · 0 评论 -
解决 110 道 LeetCode 算法问题
https://labuladong.gitbook.io/algo/链接原创 2020-06-18 15:11:50 · 105 阅读 · 0 评论 -
【C++ 取模易错点】由于答案可能会很大,请你将结果对1e9+7取模后再返回
转载 2020-06-17 16:33:17 · 2503 阅读 · 0 评论 -
11. 盛最多水的容器(leetcode)
class Solution {public: int maxArea(vector<int>& height) { int i=0; int j=height.size()-1; int res=0; while(i < j){ if(height[i] < height[j]){ res = max(res, height[i.原创 2020-06-12 21:36:36 · 103 阅读 · 0 评论 -
最短工期(拓扑排序)
参考链接:https://blog.csdn.net/weixin_43581819/article/details/104121800?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-4输入样例 1:9 120 1 60 2 40 3 51 4 12 4 13 5 25 4 04 6 94 7 75 7 46 8 27 8 4输出样例 1:18输入样例 2:4 50 1 10 2 22 1原创 2020-06-10 16:31:23 · 1057 阅读 · 0 评论 -
[面试真题]-具有依赖关系的任务执行(拓扑排序)
题目要求任务之间有一定的依赖关系,若b依赖于a,则a任务执行完成之后,b才能执行。所有任务都同时到达且串行执行,求任务的最小平均响应时间。输入实例:7 72 2 4 2 3 6 10 10 21 32 33 45 66 4输出114 3 2 0输出用例25 6 1 2 1 1 1 1 2 1 31 42 53 54 5输出232 1代码:#include <iostream>#include <algorithm>#原创 2020-06-10 16:07:02 · 2675 阅读 · 1 评论 -
是否是平衡二叉树
题目描述输入一棵二叉树,判断该二叉树是否是平衡二叉树。在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树class Solution {public: bool isBalanced=true; bool IsBalanced_Solution(TreeNode* pRoot) { getDepth(pRoot); return isBalanced; } int getDepth(TreeNode* root){原创 2020-06-06 15:50:15 · 79 阅读 · 0 评论 -
找出中序遍历顺序的下一个结点并且返回
题目描述给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。/*struct TreeLinkNode { int val; struct TreeLinkNode *left; struct TreeLinkNode *right; struct TreeLinkNode *next; TreeLinkNode(int x) :val(x), left(NULL), right(原创 2020-06-06 15:49:14 · 161 阅读 · 0 评论 -
根据前序遍历和中序遍历,重建二叉树
题目描述输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNod原创 2020-06-06 15:47:29 · 142 阅读 · 0 评论 -
按照之字形打印二叉树
题目描述请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Sol原创 2020-06-05 15:17:03 · 65 阅读 · 0 评论 -
按之字形顺序打印二叉树
题目描述请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Sol原创 2020-06-04 11:57:38 · 95 阅读 · 0 评论 -
求二叉树的镜像(非递归&递归)
左右子树对调一下就是二叉树的镜像了#include <iostream>#include <algorithm>#include <cstring>#include<string>#include<vector>#include<cmath>#include<stack>#include<queue>using namespace std;string str="543###6#7##";i原创 2020-06-03 21:23:01 · 149 阅读 · 0 评论 -
链表--如何检测一个链表是否有环及如何找到环的入口点
思路:设置一个快指针fast,一个慢指针slow,二者初始都指向链表头,fast一次走两步,slow一次走一步,两个指针同时向前移动,每移动一次,两个指针都要进行比较,如果快指针等于慢指针,则证明这是个有环的单链表,否则如果fast先行到达链表尾部或为NULL,则证明这是个不带环的单链表。如何找到环的入口点思路:如果单链表有环,当slow指针和fast指针相遇时,slow指针还没有遍历完链表,而fast指针已经在环内循环n(n>=1)圈了,假设此时slow指针走了s步,fast指针走了2s步,转载 2020-06-03 19:17:28 · 134 阅读 · 0 评论 -
《剑指 offer》反转链表
牛客网输入一个链表,反转链表后,输出新链表的表头。迭代/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/class ListNode* rreverseList(ListNode* head) { ListNode* newHead = new ListNode(-1); while(head != NULL) {原创 2020-06-03 12:17:00 · 85 阅读 · 0 评论 -
Kruskal 算法
#include <iostream>#include <algorithm>#include <cstring>#include<string>#include<vector>#include<cmath>#include<stack>#include<queue>using namespace std;int par[100];void Init(int m){ for(int i=0;原创 2020-06-02 12:20:03 · 407 阅读 · 0 评论 -
prim算法
#include <iostream>#include <algorithm>#include <cstring>#include<string>#include<vector>#include<cmath>#include<stack>#include<queue>using namespace std;//int a[10][10]={// {0,11,IFY,IFY,IFY,9,I原创 2020-06-02 11:05:55 · 127 阅读 · 0 评论 -
最小堆的创建,删除,增加(mycode)
#include <iostream>#include <algorithm>#include <cstring>#include<string>#include<vector>#include<cmath>#include<stack>#include<queue>using namespace std;#define Max 10 int a[Max]={7,20,25,13,30,5,原创 2020-05-31 17:00:34 · 131 阅读 · 0 评论 -
根据先序遍历创建树,遍历
根据先序遍历创建树输入543###6#7###include <iostream>#include <algorithm>#include <cstring>#include<string>#include<vector>#include<cmath>#include<stack>#include<queue>using namespace std;struct Node{ char v原创 2020-05-31 11:59:23 · 170 阅读 · 0 评论 -
青蛙过河(动态规划)
输入61 2 4 1 4 2输出2#include <iostream>#include <algorithm>#include <cstring>#include<string>#include<vector>#include<cmath>#include<stack>#include<queue>using namespace std;struct Node{ int x;.原创 2020-05-31 10:39:49 · 813 阅读 · 0 评论 -
最常见的面试算法题有哪些
知乎链接CSDN原创 2020-05-30 14:48:00 · 308 阅读 · 0 评论 -
最小m段和 经典dp问题
代码:#include <stdio.h> int dp[100][100] ; //dp[i][j]存储0~i的j个分组的和的最大值的最小值 int MAX(int n1, int n2){ if(n1 < n2) return n2 ; else return n1 ;} void calculate(int a[], int n, int m){ int i, j, k, temp, min ; if(n == 0){转载 2020-05-30 12:15:18 · 393 阅读 · 2 评论 -
经典DP问题合集
转发来自一、上台阶问题二、矩阵最小路径和三、最长递增子序列求一个序列的最长递增子序列,这样的子序列是允许中间越过一些字符的,即留“空”。例如:4 2 3 1 5 的最长递增子序列为 2 3 5,长度为 3 。解法:解法这里给出两种动态规划的做法,第二种是比较优化的 dp四、最长公共子序列最长公共链接回溯五、背包问题dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i])...转载 2020-05-30 12:03:25 · 1133 阅读 · 0 评论 -
vector C++
1:初始化方式1.//定义具有10个整型元素的向量(尖括号为元素类型名,它可以是任何合法的数据类型),不具有初值,其值不确定vectora(10);方式2.//定义具有10个整型元素的向量,且给出的每个元素初值为1vectora(10,1);方式5.//从数组中获得初值int b[7]={1,2,3,4,5,6,7};vector a(b,b+7);#include<vector>vector<int> a,b;//b为向量,将b的0-2个元素赋值给向量a转载 2020-05-19 16:46:59 · 86 阅读 · 0 评论 -
快速排序算法(my)
#include <iostream>using namespace std;int quickpartty(int a[],int l,int r){ int x=l; int y=r; int tag=a[x]; while(x<y){ while(tag<=a[y]&&x<y){ y--; } if(x<y){ a[x]=a[y]; x++; } while(tag>=a[x]&&x原创 2020-05-19 16:35:07 · 94 阅读 · 0 评论 -
链表的插入删除
#include <iostream>using namespace std;#include <algorithm>#include <vector>#include <string>#include <cstring>#include <queue>struct ListNode{ int m_nKey; ListNode* m_pNext;};//struct ListNode {//原创 2020-05-19 12:24:15 · 72 阅读 · 0 评论 -
找出单向链表中的一个节点,该节点到尾指针的距离为K
找出单向链表中的一个节点,该节点到尾指针的距离为K。链表的倒数第0个结点为链表的尾指针。要求时间复杂度为O(n)。链表结点定义如下:struct ListNode{int m_nKey;ListNode* m_pNext;}链表节点的值初始化为1,2,3,4,5,6,7。输入描述:该节点到尾指针的距离K输出描述:返回该单向链表的倒数第K个节点,输出节点的值输入例子1:2输出例子1:6#include <iostream>using namespace std;原创 2020-05-19 12:06:42 · 314 阅读 · 0 评论 -
网易实习生笔试编程题 二叉树
有一棵二叉树,树上每个点标有权值,权值各不相同,请设计一个算法算出权值最大的叶节点到权值最小的叶节点的距离。二叉树每条边的距离为1,一个节点经过多少条边到达另一个节点为这两个节点之间的距离。#include <iostream>using namespace std;#include <algorithm>#include <vector>#include <string>#include <cstring>#include <原创 2020-05-18 19:22:37 · 243 阅读 · 0 评论 -
最小堆的创建,删除,增加
#include <iostream>#define Max 10 using namespace std;int n;int a[Max]={7,20,25,13,30,5,7,8}; void adjust_heap(int* a,int s,int m){ int as=a[s]; for(int j=2*s;j<=m;j*=2){ if(j<m&&a[j]原创 2020-05-15 18:31:14 · 142 阅读 · 0 评论 -
快速排序算法
#include <iostream>using namespace std;//数组打印void P(int a[],int n){ for(int i=0; i<n; i++) cout<<a[i]<<" "; cout<<endl;}int quickSortPartition(int s[], int l, int r){ //Swap(s[l], s[(l + r) / 2]); //若以原创 2020-05-15 16:58:05 · 134 阅读 · 0 评论 -
求一个集合的所有子集(lintCode 17.子集)
#include <iostream>using namespace std;#include <algorithm>#include <vector>#include <string>#include <cstring>#include <math.h>int main(){ int set[5]={1,4,2,6,8};// int x;// x=6;// x>>=1;// cout<<原创 2020-05-15 12:14:03 · 125 阅读 · 0 评论 -
树的创建,遍历
#include <iostream>using namespace std;#include <algorithm>#include <vector>#include <string>#include <cstring>#include <queue>class Node{ public: int val; Node *left, *right; Node()原创 2020-05-14 19:33:34 · 77 阅读 · 0 评论 -
C++11 string int转化
#include #include using namespace std;std::string str1 = "45";int myint1 = std::stoi(str1);double f = 23.43;std::string f_str = std::to_string(f);原创 2020-05-12 12:07:31 · 638 阅读 · 0 评论 -
快速排序
该方法的基本思想是:1.先从数列中取出一个数作为基准数。2.分区过程,将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边。3.再对左右区间重复第二步,直到各区间只有一个数。 `//快速排序void quick_sort(int s[], int l, int r){if (l < r){//Swap(s[l], s[(l + r) / 2]); //将中...原创 2020-03-22 09:49:20 · 75 阅读 · 0 评论 -
tsp问题 模拟退火算法
#include#include#include&lt;stdlib.h&gt;#include&lt;stdio.h&gt;#include&lt;time.h&gt;using namespace std;int Length(int **d,int *node,int n);int main(){int v=0.9; //衰减函数 α(t)=0.9 * t0int t0原创 2018-11-03 22:02:46 · 286 阅读 · 0 评论