leetcode
多多的伴随矩阵
这个作者很懒,什么都没留下…
展开
-
ACWING-844-走迷宫
广度优先#include <bits/stdc++.h>using namespace std;typedef pair<int,int> PII;const int N = 110;int n, m;int g[N][N]; //地图int f[N][N]; //距离PII q[N*N]; //模拟队列int n,m;void bfs (int a, int b){ queue<PII> q; q.push({a,...原创 2022-05-28 10:29:40 · 76 阅读 · 0 评论 -
ACWING-843-n 皇后问题
难点:正反对角线的坐标表示#include <iostream>using namespace std;const int N = 20;int n;// g[N][N]用来存路径char g[N][N];// col列, dg对角线, udg反对角线bool col[N], dg[N], udg[N];void dfs (int u) { // 递归终止, 搜了n行 if (u == n) { // 输出 for ...原创 2022-05-22 14:27:58 · 116 阅读 · 0 评论 -
ACwing-841-字符串哈希
比较不同区间的子串是否相同,就转化为对应的哈希值是否相同X1X2X3⋯Xn−1Xn 的字符串,映射公式 (X1×Pn−1+X2×Pn−2+⋯+Xn−1×P1+Xn×P0)modQ通过巧妙设置P (131 或 13331) , Q 64的值,一般可以理解为不产生冲突区间和公式 h[l,r]=h[r]−h[l−1]×Pr−l+1如何理解理解该...原创 2022-05-19 16:01:30 · 190 阅读 · 0 评论 -
Acwing-840-模拟散列表
关键:数组模拟链表的遍历// 840. 模拟散列表#include <cstring>#include <iostream>using namespace std;const int N = 1e5 + 3;int n;// h[]是哈希函数的一维数组int h[N]; // e[]是链表中存的值int e[N]; // ne[]是指针存的 指向 的地址int ne[N]; int idx;void insert(int x) { ...原创 2022-05-17 01:25:05 · 243 阅读 · 0 评论 -
ACwing-836-合并集合-并查集
/* Acwing 836 并查集*/#include <iostream>using namespace std;const int N = 100010;int p[N];// 查找根节点int find(int x){ // 只有根节点满足p[x] == x,递归查到根 if(p[x] != x) p[x] = find(p[x]); return p[x];}int main(){ // 1. 输入n, m int...原创 2022-05-01 14:41:39 · 131 阅读 · 0 评论 -
并查集知识点
查原创 2022-04-28 13:35:10 · 274 阅读 · 0 评论 -
ACWing- 800 -数组元素的目标和
如下图方式进行遍历。与纯粹暴力的双重循环做法相比,j指针不会回退,代码如下#include <iostream>#include <algorithm>using namespace std;const int N = 1000010;int n, m, x;int a[N], b[N];int main(){ // 1. 按照规则输入 scanf("%d%d%d", &n, &m, &x); for (int...原创 2022-04-23 22:49:58 · 115 阅读 · 0 评论 -
ACwing-830-单调栈
在这里插入代码片原创 2022-04-19 10:48:15 · 107 阅读 · 0 评论 -
Acwing-数组中的逆序对788-巧用归并的流程中
#include <iostream>using namespace std;typedef long long ll;const int N = 100010;int n;int q[N],tmp[N];LL merge_sort(int l, int r){ if(l >= r) return 0; int mid = l + r >> 1; LL res = merge_sort(l,mid) + merge_sort(m...原创 2022-04-17 14:17:05 · 68 阅读 · 0 评论 -
ACwing-786-第k个数-快排思想
快排思想的运用。因为快排每次可以确定一个元素的最终位置,那么只要确定了第k个数字的位置,递归就可以结束了。// 第k个数#include <iostream>using namespace std;const int N = 100010;int n, k, a[N];int quickFind (int l, int r, int k) { // 1. 前半部分和快排完全相同: 找到第k个位置了 if (l == r ) return a[r]; ...原创 2022-04-16 12:17:02 · 80 阅读 · 0 评论 -
Acwing位运算-801-二进制1的个数
小例子原创 2022-04-12 08:34:43 · 217 阅读 · 0 评论 -
Acwing-799-最长连续不重复子序列
创建一个数组s[N]去记录[j,i]之间元素出现的次数,当出现重复元素就记录当前长度,然后移动j,直到元素不再重复#include <iostream>using namespace std;const int N = 100010;int n;int a[N],s[N];int main(){ // cin >> 变量名,输入给谁 cin >> n; for (int i = 0; i < n; i ++) cin &...原创 2022-04-11 21:37:26 · 91 阅读 · 0 评论 -
ACwing-797-差分
解析https://www.acwing.com/solution/content/26588/#include<iostream>using namespace std;const int N = 1e5 + 10;int a[N], b[N];int main(){ int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { //...原创 2022-04-11 15:33:18 · 214 阅读 · 0 评论 -
Acwing-796-子矩阵的和
把矩阵s[i][j]存起来计算公式: s[i][j] = s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];// 796. 子矩阵的和 #include<bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1010;int n,m,q;ll a[N][N], s[N][N];int main(){ scanf("%d%d%d", &n,...原创 2022-04-11 08:09:35 · 157 阅读 · 0 评论 -
ACwing-795-前缀和
数组求个和记录下来,拿去用。关键// 2. 记录s[i]for (int i = 1; i <= n; i ++) s[i] = s[i-1] + a[i];// 4. 算一下printf(“%d\n”, s[r]-s[l - 1]);#include <iostream>using namespace std;const int N = 100010;int n,m;int a[N], s[N];int main(){ scanf("%d%d",..原创 2022-04-10 15:36:21 · 49 阅读 · 0 评论 -
ACwing-794-高精度除法
高精度思路都类似,就计算的时候,每一位的处理上有差别#include <iostream>#include <vector>#include <algorithm>using namespace std;vector<int> div(vector<int> &A, int b, int &r){ vector<int> C; r = 0;// 从数组的最高位-数字的最高位 for (in...原创 2022-04-10 14:56:59 · 214 阅读 · 0 评论 -
ACwing-793-高精度乘法
A的每一位去和b相乘,有点分配律的感觉#include <iostream>#include <vector>using namespace std;int main(){ // 1. 输入 string a; int b; cin >> a >> b; vector<int> A; // 2. 个位放在索引0的位置 for (int i = a.size() - 1; i...原创 2022-04-10 09:39:27 · 62 阅读 · 0 评论 -
ACwing-792-高精度减法
仍然是逆序存储保证大数减去小数减法有借位规则#include <iostream>#include <vector>using namespace std;const int N = 1e6 + 10;int main (){ // 1. 用字符串读进来 string a , b; // 2. 存储到vector里面去 vector<int> A, B; cin >> a >>..原创 2022-04-09 15:52:51 · 127 阅读 · 0 评论 -
ACwing-791-高精度加法
都存在数组里,每一位每一位相加#include <iostream>#include <vector>using namespace std;const int N = 1e6 + 10;int main (){ // 1. 用字符串读进来 string a , b; // 2. 存储到vector里面去 vector<int> A, B; cin >> a >> b; // 3. ..原创 2022-04-09 13:58:38 · 102 阅读 · 0 评论 -
Acwing-790-数的三次方根-浮点数二分
简单的二分问题,通过缩小l和r (即r-l < 1e-7) 将所求的结果夹出来#include <iostream>#include<iomanip>using namespace std;// 1. 声明左右边界,并且赋值double l = -10000;double r = 10000;double mid = (l+r)/2;int main(){ double num; cin>>num; while(r ..原创 2022-04-08 08:13:53 · 74 阅读 · 0 评论 -
ACwing-789-数的范围-二分
二分最重要的两段性,即数据可以按照某种标准区分开,而不是单调性如果我们要找红色的边缘点mid = (l+r)/2 取中间值;如果中间值符合红色的性质,则说明红色的边缘点在[mid,r]范围里,更新l = mid, mid有可能是我们要找的边缘点;如果中间值不符合红色性质,则说明点在[l, mid -1]里,更新r = mid -1.如果我们要绿色的边缘点mid = (l + r)/2取中间值如果中间值符合绿色的性质,则说明绿色的边缘点在[l,mid]范围里,更新r = mi.原创 2022-04-07 20:19:45 · 123 阅读 · 0 评论 -
ACwing-787-归并排序
代码亲测可AC归并排序:将两个分别有序的序列,合并成一个有序序列#include <iostream>using namespace std;const int N = 100000000;int n;// 归并需要额外的辅助数组int q[N], tmp[N];void merge_sort(int q[], int l, int r){ if ( l >= r) return; int mid = l + r >> 1; // 1.原创 2022-04-04 21:22:51 · 108 阅读 · 0 评论 -
ACwing-785-快速排序
确定分界点、枢轴元素 x = q [l + r >> 1]调整区间递归给左边排序,递归地给右边排序// 挑战模式,代码可AC# include<iostream>using namespace std;const int N = 1e6 + 10;int n; int q[N];void quick_sort(int q[], int l, int r){// 1. 终止条件 if (l >= r) return;// 2. 单次 .原创 2022-04-04 11:33:57 · 79 阅读 · 0 评论 -
leetcode-102. 二叉树的层序遍历
https://leetcode-cn.com/problems/invert-binary-tree/submissions/class Solution {public: vector<vector<int>> levelOrder(TreeNode* root) { // 队列 queue<TreeNode *> que; // 根入栈 if (root != NULL ) que.push原创 2022-01-23 17:48:41 · 308 阅读 · 0 评论 -
leetcode-94 二叉树的中序遍历
https://leetcode-cn.com/problems/binary-tree-inorder-traversal/class Solution {public: vector<int> inorderTraversal(TreeNode* root) { vector<int> result; stack<TreeNode*> st; TreeNode* cur = root; whi原创 2022-01-17 21:27:13 · 128 阅读 · 0 评论 -
leetcode-77 组合
https://leetcode-cn.com/problems/combinations/submissions/class Solution {public:// [1,n]// k个数 //总的结果和每次结果 vector <vector<int>> result; vector<int> path; void backtracing(int n, int k, int startInde原创 2022-01-16 17:54:17 · 157 阅读 · 0 评论 -
leetcode-144 二叉树的前序遍历
https://leetcode-cn.com/problems/binary-tree-preorder-traversal/class Solution {public: vector<int> preorderTraversal(TreeNode* root) { stack<TreeNode*> st; vector<int> result; if (root == NULL) return result原创 2022-01-14 22:29:27 · 71 阅读 · 0 评论 -
Leetcode-232 用栈实现队列
https://leetcode-cn.com/problems/implement-queue-using-stacks///stIn完全用来压入数据//stOut是stIn的反序,用来弹出数据class MyQueue {public: stack<int> stIn; stack<int> stOut;public: MyQueue() { } // 入栈进入stIn void push(int x) {原创 2022-01-14 20:48:36 · 3306 阅读 · 0 评论