自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(64)
  • 资源 (1)
  • 问答 (1)
  • 收藏
  • 关注

原创 24. 两两交换链表中的节点

【代码】24. 两两交换链表中的节点。

2023-07-11 02:30:25 58

原创 23合并k个升序链表

学习了优先权队列的使用方法,引入头文件,写一个比较函数,可以当作最小堆、最大堆使用。

2023-07-06 23:50:11 89

原创 22括号生成

【代码】22括号生成。

2023-07-02 15:06:04 60

原创 蓝桥杯备赛默写本

二维vector初始化vector<vector<int>>a(n,vector<int>(m,0));头文件#include<bits/stdc++.h>链表构建struct ListNode { int val; ListNode*next; ListNode():val(0),next(nullptr){} ListNode(int x):val(x),next(nullptr){} ListNode(int x, ListNod

2022-06-08 21:32:15 69

原创 蓝桥杯训练第二场

扫雷游戏#include<bits/stdc++.h>using namespace std;int main(){ int m,n; string c; scanf("%d%d",&n,&m); vector<vector<char>>a(n+3, vector<char>(m+3, '?')); for(int i=0;i<n;i++){ cin>>c; //scanf("%s",&c);

2022-06-08 21:31:52 71

原创 第十届蓝桥杯

组队492 490脑子一抽写错了,不应该对列穷举,其实手算就该出了#include<bits/stdc++.h>using namespace std;int a[20][6];int main(){ int i1,i2,i3,i4,i5; for(int i=0;i<20;i++){ for(int j=0;j<6;j++){ cin>>a[i][j]; } } int maxn=0; int l=0; for(i1=0;i1&l

2022-04-14 20:15:11 125 1

原创 十一届蓝桥杯省赛第二场

门牌制作#include<iostream>using namespace std;int main(){ int ans=0; for(int i=1;i<2021;i++){ int m=i; while(m!=0){ if(m%10==2){ ans++; } m=m/10; } } cout<<ans; return 0; }624既约分数#include<iostream>using nam

2022-04-07 11:45:56 78

原创 十二届蓝桥杯省赛

空间67,108,864卡片#include<iostream>using namespace std;int main(){ int ans=0,flag=1; int t; int a[10]; for(int i=0;i<=9;i++){ a[i]=2021; } for(int n=1;flag==1;n++){ t=n; while(t!=0){ a[t%10]--; if(a[t%10]<0){ flag=0;

2022-04-06 19:41:24 100

原创 70 746爬楼梯

两题都是基础dp题#include<iostream>#include<vector>using namespace std;class Solution {public: int climbStairs(int n) { if (n == 1)return 1; if (n == 2)return 2; vector<int>dp(n + 1); dp[1] = 1; dp[2] = 2;

2022-03-27 11:27:54 43

原创 509 1137斐波那契数列

第一题,常规斐波那契数列,学到了很多东西,书上的递归其实是一种效率很低的做法,因为他重复计算了很多子问题,所以用自顶向下的备忘录法或者自底向上的动态规划法效果更好。#include<iostream>#include<vector>using namespace std; class Solution {//传统递归,耗时大public: int fib(int n) { if (n == 0)return 0; else if (

2022-03-21 22:53:31 211

原创 19移除链表的倒数第N个结点

这题可以用快慢指针的方法,快指针到终点时,慢指针应该在删除结点的前一个结点,为了防止删除头节点,应该在头节点前面新建一个before结点,返回的时候也应返回before->next,这题题解里还用了二次遍历、用栈来解决两种方法,当然还是快慢指针是最好的。#include<iostream>#include<vector>#include<algorithm>using namespace std;struct ListNode { int val;

2021-11-29 21:38:41 139

原创 18 四数之和

类似于三数之和,首先排序,确定前两位之和用双指针确定后两位,注意重复问题的解决。#include<iostream>#include<vector>#include<algorithm>using namespace std;class Solution {public: vector<vector<int>> fourSum(vector<int>& nums, int target) {

2021-11-29 15:47:49 55

原创 235二叉树的最近公共祖先

因为是从根节点开始的,如果root在pq之间,那root就是最近公共祖先。#include<iostream>#include<queue>#include<vector>#include<unordered_set>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0),

2021-11-26 22:36:01 350

原创 653两数之和IV

二叉搜索树中的两数之和,通过遍历得到一个vector数组,转化出1两数之和问题。#include<iostream>#include<queue>#include<vector>#include<unordered_set>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0

2021-11-26 22:03:07 338

原创 98验证二叉搜索树

验证的方法并不是每个节点都大于左边的,小于右边的,而是每个节点必须大于左子树每一个节点,小于右子树每一个节点。方法一是给每个节点赋予一个上限和一个下限,每次判断节点值是否在界内。方法二是看了一篇题解,根据二叉搜索树中序遍历后就是递增数列的原理进行。这个递归用的很妙#include<iostream>#include<queue>#include<vector>using namespace std;struct TreeNode { int val;

2021-11-26 20:48:17 425

原创 701 二叉搜索树中的插入操作

二叉搜索树的插入,只要到规定的节点位置新建一个叶节点就可以了。#include<iostream>#include<queue>#include<vector>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {}

2021-11-26 19:52:09 235

原创 700二叉树中的搜索

根据二叉树的性质,很好判断#include<iostream>#include<queue>#include<vector>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {} TreeNode(int x) :

2021-11-26 19:21:42 289

原创 112路径总和

刚开始没注意必须是根节点到叶节点,所以必须多一个是不是叶节点的判定,程序整体用递归实现。#include<iostream>#include<queue>#include<vector>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nu

2021-11-26 19:07:47 49

原创 226反转二叉树

核心在于,如果一个节点的子节点的子节点都已经交换顺序了,那么就交换该节点的左右子节点。#include<iostream>#include<queue>#include<vector>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nul

2021-11-26 18:41:23 57

原创 101对称二叉树

每次比较两个对称指针节点的值,可以设置两个指针,他们每次都一个向左,一个向右,每次递归来实现。#include<iostream>#include<queue>#include<vector>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right

2021-11-26 18:14:26 423

原创 104二叉树的深度

二叉树的深度用深度优先搜索可以很快的写出来#include<iostream>#include<queue>#include<vector>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {} TreeNode(

2021-11-24 22:51:52 169 1

原创 102二叉树的层序遍历

层序遍历通过一个 队列实现,循环直到队列为空为止#include<iostream>#include<queue>#include<vector>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {} TreeNo

2021-11-23 23:49:45 62 1

原创 145 二叉树的后序遍历

#include<iostream>#include<vector>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {} TreeNode(int x) : val(x), left(nullptr), right(nullpt

2021-11-22 20:17:29 41

原创 94二叉树的中序遍历

和前面前序遍历差不多#include<iostream>#include<vector>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {} TreeNode(int x) : val(x), left(nullptr), r

2021-11-22 20:06:08 39

原创 144二叉树的前序遍历

用递归就按书上的来就行,题解也提供了迭代的方法,太麻烦。#include<iostream>#include<vector>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {} TreeNode(int x) : val(x)

2021-11-22 20:02:14 34

原创 232用栈实现队列

我有一个问题一开始没反应过来,就是s2的栈顶一定是队列队头的。如果s2空了,s1会把全部元素转到s2#include<iostream>#include<stack>using namespace std;class MyQueue {private: stack<int>s1, s2; void intoout() { while (!s1.empty()) { s2.push(s1.top());

2021-11-21 15:58:19 437

原创 20有效的括号

用栈来处理我提一次提交没通过是因为忽略了栈为空时,输入一个右括号的情况#include<iostream>#include<stack>#include<unordered_map>using namespace std;class Solution {public: bool isValid(string s) { stack<char>sstack; unordered_map<char, cha

2021-11-21 14:40:20 154

原创 83删除排序链表中的重复元素

没看清题目,题目的链表是升序的,所以重复元素都是连续出现的,不应该用这个set的,直接判断自身和下一个就好了。#include<iostream>#include<unordered_set>using namespace std;struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), n

2021-11-21 13:52:57 41

原创 206反转列表

递归和迭代两种方法,我觉得就链表而言迭代更有训练价值#include<iostream>using namespace std;struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {} ListNode(int x, ListNode *next) : val(x

2021-11-20 19:57:09 297

原创 203移除链表元素

要么递归要么迭代,但是题解的递归感觉永远写的很简单#include<iostream>using namespace std;struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {} ListNode(int x, ListNode *next) : val(x)

2021-11-20 19:29:19 37

原创 21合并两个有序链表

我写的复杂了,主要原因是没有意识到一个问题,就是链表的链接只需要接一个就行了,而不需要一个节点一个节点的去接。题解有两种方法,迭代和递归#include<iostream>using namespace std;struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {}

2021-11-20 15:11:10 718

原创 141环形链表

我用的是一个常见的方法,就是用一个集合记录节点是否已经被记录过。题解用了一个龟兔赛跑方法,又见Floyd判圈算法,就是如果起点在前面的快指针如果追上了慢指针,说明有环。#include<iostream>#include<unordered_set>using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}

2021-11-20 13:25:22 161

原创 Leetcode刷题阶段性小结01

十月到现在刷了30道题了,做一下阶段小结题目列表1两数之和学会了unordered_map容器的基本操作2两数相加这题考察了链表的基本操作,有一个当时没搞懂的点是nullptr,现在明白是空指针的意思,因为null重载调用可能会发生错误3无重复字符的最长子串用一个set来判断有没有重复字符,另外用了滑动窗口方法,以当前最长的长度为窗口长度,每次将左指针向右移一位,然后右指针移动能移动的最大距离。9回文数这题我错了很多次,主要是一些特例输入太多了,最后题解的return方式很巧妙7整数反转

2021-11-19 14:00:38 194

原创 242有效的字母异位词

这题和383赎金信有点像我用了两个哈希表,题解只用了一个,然后对另一操作时,使用–操作,还挺有启发性的。#include<iostream>#include<unordered_map>using namespace std;class Solution {public: bool isAnagram(string s, string t) { unordered_map<char, int>smap; unordere

2021-11-18 22:07:57 166

原创 383赎金信

问题相当于一个字符串的字符是否是另一个字符串字符串字符的子集。我用了两个哈希表统计字符出现的次数。#include<iostream>#include<unordered_map>using namespace std;class Solution {public: bool canConstruct(string ransomNote, string magazine) { unordered_map<char, int>rn;

2021-11-18 21:15:24 71

原创 387字符串中的第一个唯一字符

我用的方法是遍历两次,用一个哈希表存储每个字符出现的次数题解用了一个哈希表存储字符索引还有一个队列的方法,因为我对容器还不熟悉,有空再写#include<iostream>#include<unordered_map>using namespace std;class Solution {public: int firstUniqChar(string s) { unordered_map<char,int>map;

2021-11-18 12:30:26 50

原创 73矩阵置零

用一个m+n的空间消耗可以轻松处理,只要记录每一行每一列是否有0就行了,题解中给出了空间复杂度更低的处理方式,把第一行和第一列作为记录位置,但是要标记位记录第一行第一列本身是否有0.。还可以只把第一列是否有0记录,第一行在行里最后更新,因为如果第一行作为标记行,如果先更新会影响别的位的更新#include<iostream>#include<vector>using namespace std;class Solution {public: void setZer

2021-11-18 11:39:02 168

原创 36有效的数独

我用三个9*9的矩阵存储9行,9列,9个box里,1~9出现过的情况,值得注意的是,board[i][j]是char各式,想要其作为下标索引,需要做这步处理row[i][board[i][j]-'0'-1] ++;完整代码如下#include<iostream>#include<vector>using namespace std;class Solution {public: bool isValidSudoku(vector<vector<ch

2021-11-18 10:32:18 50

原创 17电话号码的字母组合

看到所有组合就应该明白这题考察了搜索算法,可以使用DFS或者BFS数字对应字母用了一个哈希表实现,主体部分看了题解,自己加了点注释,主要之前没怎么用过DFS。#include<iostream>#include<vector>#include<unordered_map>using namespace std;class Solution {public: vector<string> letterCombinations(string

2021-11-17 11:18:31 65

原创 118杨辉三角

考察了一些二维vector的使用方法,我和题解写的差不多,题解里用了一个resize函数调整vector大小。#include<iostream>#include<vector>using namespace std;class Solution {public: vector<vector<int>> generate(int numRows) { vector<vector<int>>ans(num

2021-11-16 21:27:20 209

复习要点2020-2021-2.doc

NJPUT算法复习,自己期末考试整理的

2021-09-25

LINGO错误191

2021-07-25

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除