俺到处练练
自信的小螺丝钉
这个作者很懒,什么都没留下…
展开
-
AcWing 第48场周赛 4412. 构造数组
原题链接:AcWing 第48场周赛 4412. 构造数组第一次写Acwing的周赛,前两道题挺简单,这道题也不难,考场上交上去的代码过了10/16,我以为我哪里逻辑有错,结果下考了才知道是我没有定义long long ,用的int,AC就这么远离了我,心痛。#include <bits/stdc++.h>using namespace std;const int N=1e6+10;const int mod=998244353 ;#define ll long longint原创 2022-04-23 21:27:00 · 446 阅读 · 0 评论 -
AcWing 第48场周赛 4411. 三仙归洞
原题链接:AcWing 第48场周赛 4411. 三仙归洞提前写好6种状态#include <bits/stdc++.h>using namespace std;int a[6][3]={{0,1,2},{1,0,2},{1,2,0},{2,1,0},{2,0,1},{0,2,1}};int main(){ int n,x; cin>>n>>x; int t=n%6; cout<<a[t][x]; retur原创 2022-04-23 20:53:21 · 189 阅读 · 0 评论 -
AcWing 第48场周赛 4410. 吃鸡蛋
原题链接:AcWing 第48场周赛 4410. 吃鸡蛋#include <bits/stdc++.h>using namespace std;int main(){ int n,m; cin>>n>>m; int res=n; int egg=0; for(int i=m;i<=n;i+=m) egg++; for(int i=n+1;i<100000000;i++) {原创 2022-04-23 20:50:10 · 178 阅读 · 1 评论 -
第十二届蓝桥杯省赛C_C++ 直线
#include <bits/stdc++.h>using namespace std;vector<pair<double,double>> v;int main(){ std::ios::sync_with_stdio(false); for(int i=0;i<20;i++) { for(int j=0;j<21;j++) { for(int k=0;k<2.原创 2022-04-08 22:48:26 · 603 阅读 · 0 评论 -
剑指 offfer JZ36 二叉搜索树与双向链表
原题链接:剑指 offfer JZ36 二叉搜索树与双向链表中序遍历:时间复杂度:O(N),等于中序遍历的时间复杂度。空间复杂度:O(N),开辟了一个数组来存储结点。/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public原创 2022-03-27 15:23:27 · 76 阅读 · 0 评论 -
剑指offer JZ34 二叉树中和为某一值的路径(二)
原题链接:剑指offer JZ34 二叉树中和为某一值的路径(二)开始自作主张在dfs里写了个剪枝:if(sum>=expectNumber) return ;,,,,题目里说了节点值有可能为负数,把剪枝去掉就对了/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*原创 2022-03-27 14:26:45 · 1584 阅读 · 0 评论 -
剑指offer JZ33 二叉搜索树的后序遍历序列
原题链接:剑指offer JZ33 二叉搜索树的后序遍历序列思想1:分治1.二叉树的后序遍历顺序是:左子树 -> 右子树 -> 根节点,因此序列的最后一个数代表了根节点2. 我们可以将一个序列划分为3段, 左子树+右子树+根, 例如[4, 8, 6, 12, 16, 14, 10]可以根据根节点的值将其划分为左子树[4, 8, 6], 右子树[12, 16, 14], 根[10], 由于我们是先确定的右子树区间, 因此当左子树区间中出现大于根节点的值时, 序列不合法, 我们再采用分治的思想原创 2022-03-27 14:02:15 · 1033 阅读 · 0 评论 -
使用dfs实现出栈的所有可能
例如,输入数列为{1,2,3,4,5},输出出栈的所有可能:代码:#include <bits/stdc++.h>using namespace std;vector<int > tmp;vector<int> in={1,2,3,4,5};stack<int> s;int cnt=0;void dfs(){ if(tmp.size()==in.size()) { for(int i=0;i<tmp.si原创 2022-03-26 22:06:10 · 394 阅读 · 0 评论 -
剑指offer JZ31 栈的压入、弹出序列
原题链接:剑指offer JZ31 栈的压入、弹出序列 思想:1.初始化, 用指针i指向pushV的第一个位置, 指针j指向popV的第一个位置。2.如果pushV[i] != popV[j], 那么应该将pushV[i]放入栈中, ++i3.否则,pushV[i]==popV[j], 说明这个元素是放入栈中立马弹出,所以,++i, ++j,然后应该检查popV[j]与栈顶元素是否相等,如果相等,++j, 并且弹出栈顶元素4.重复2,3, 如果i==pushV.size(), 说明入栈序列访问原创 2022-03-26 21:20:46 · 439 阅读 · 0 评论 -
剑指offer JZ29 顺时针打印矩阵
原题链接:剑指offer JZ29 顺时针打印矩阵怎么说,这都能过?我的垃圾代码。。。我总是把自己绕晕。class Solution {public: vector<int> printMatrix(vector<vector<int> > matrix) { int n=matrix.size(); int m=matrix[0].size(); vector<int> res;原创 2022-03-26 18:53:30 · 707 阅读 · 0 评论 -
剑指offer JZ28 对称的二叉树 递归
原题链接:剑指offer JZ28 对称的二叉树递归将二叉树镜像翻转,递归判断两个树是否对称。/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: TreeNode*原创 2022-03-26 17:17:40 · 167 阅读 · 0 评论 -
剑指offer JZ27 二叉树的镜像 递归/栈
原题链接:JZ26 剑指offer 二叉树的镜像递归:/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */class Solution {public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改原创 2022-03-26 16:36:05 · 76 阅读 · 0 评论 -
剑指offer JZ26 树的子结构 递归
原题链接:剑指offer JZ26 树的子结构递归递归递归递归············/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: bool fun(TreeNode* pRoot1, TreeNode* p原创 2022-03-26 13:34:01 · 178 阅读 · 0 评论 -
剑指offer JZ25 合并两个排序的链表
原题链接:JZ25 合并两个排序的链表emmmm,我总结出来了,我的解题习惯就是,自己把自己绕晕。。。。迭代写法:/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/class Solution {public: ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {原创 2022-03-26 12:26:50 · 1032 阅读 · 0 评论 -
剑指offer JZ15 二进制中1的个数
原题链接:JZ15 二进制中1的个数class Solution {public: int NumberOf1(int n) { int res=0; while(n) { n&=n-1; res++; } return res; }};原创 2022-03-25 19:42:23 · 191 阅读 · 0 评论 -
剑指offer JZ14 剪绳子 递归
原题链接:剑指offer JZ14 剪绳子我蠢到写了dfs。。。。class Solution {public: int v[100]; int cut(int n) { if(n<=4) return n; if(v[n]) return v[n]; int res=0; for(int i=1;i<n;i++) res=m原创 2022-03-25 19:33:54 · 412 阅读 · 0 评论 -
剑指offer JZ13 机器人的运动范围 dfs
原题链接:JZ13 机器人的运动范围class Solution {public: bool fun(int x,int y,int s) { int sum=0; while(x) { sum+=x%10;x/=10; } while(y) { sum+=y%10;y/=10; } if(sum<=s)原创 2022-03-24 22:35:36 · 234 阅读 · 0 评论 -
剑指 offer 旋转数组的最小数字
原题链接:旋转数组的最小数字二分查找这种二分查找难就难在,arr[mid]跟谁比.我们的目的是:当进行一次比较时,一定能够确定答案在mid的某一侧。一次比较为 arr[mid]跟谁比的问题。一般的比较原则有:如果有目标值target,那么直接让arr[mid] 和 target 比较即可。如果没有目标值,一般可以考虑端点这里我们把target 看作是右端点,来进行分析,那就要分析以下三种情况,看是否可以达到上述的目标。情况1:arr[mid] > target:4 5 6 1 2 3a转载 2022-03-24 20:17:03 · 45 阅读 · 0 评论 -
关于斐波那契数列的优化
斐波那契数列就不用多说了记录一下怎么优化代码1:class Solution {public: int Fibonacci(int n) { if(n<=2) return 1; return Fibonacci(n-1)+ Fibonacci(n-2); }};代码2:class Solution {public: int a[50]; int Fibonacci(int n) {原创 2022-03-23 17:48:10 · 196 阅读 · 0 评论 -
剑指offer JZ9 用两个栈实现队列
原题链接:JZ9 用两个栈实现队列class Solution{public: void push(int node) { stack1.push(node); } int pop() { if(stack2.empty()) { while(!stack1.empty()) { stack2.push(stack1.top());原创 2022-03-23 17:21:32 · 105 阅读 · 0 评论