![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
巴巴多斯小魔
这个作者很懒,什么都没留下…
展开
-
leetcode 399除法求值 超水带权并查集
【代码】leetcode 399除法求值 超水带权并查集。原创 2024-01-18 17:02:39 · 534 阅读 · 0 评论 -
leetcode 486. 预测赢家 dp
题目class Solution {public: bool PredictTheWinner(vector<int>& a) { int n=a.size(),dp[n][n]; for(int i=0;i<n;++i) dp[i][i]=a[i]; for(int i=0;i<n-1;++i) dp[i][i+1]=max(a[i],a[i+1])-min(a[i],a[i+1]); for(i原创 2021-07-08 14:57:03 · 123 阅读 · 0 评论 -
leetcode 160. 相交链表 唔~
题目class Solution {public: ListNode *getIntersectionNode(ListNode *x, ListNode *y) { if(!x||!y) return NULL; ListNode *xx=x,*yy=y; while(xx!=yy){ xx=(xx?xx->next:y); yy=(yy?yy->next:x); }原创 2021-04-03 01:40:07 · 75 阅读 · 0 评论 -
leetcode 143. 重排链表 123456变成162534
快慢指针找到中间节点后,后一半反转 然后交叉合并。。class Solution {public: ListNode* getmid(ListNode *head){ ListNode *slow,*fast; slow=fast=head; while(fast&&fast->next) fast=fast->next->next,slow=slow->next; return slow;.原创 2021-04-03 00:40:05 · 381 阅读 · 0 评论 -
leetcode 42. 接雨水 找到后面第一个大于他的找不到就找后面最大的 扫一遍就行。。
题目class Solution {public: int trap(vector<int>& a) { if(a.size()<=1) return 0; int n=a.size(),mx[n],sum[n]; int val=a[n-1],dex=n-1; for(int i=0;i<n;++i) sum[i]=i?(sum[i-1]+a[i]):a[i]; for(int i原创 2021-04-02 00:50:36 · 76 阅读 · 0 评论 -
leetcode 189. 旋转数组 gcd/旋转 模拟
题目class Solution {public: void reverse(vector<int>&a,int i,int j){ cout<<i<<" "<<j<<endl; while(i<j) swap(a[i],a[j]),++i,--j; } void rotate(vector<int>& a, int k) { int n原创 2021-03-03 15:14:17 · 98 阅读 · 1 评论 -
杨氏矩阵第K小/查找目标数字
leetcode 378. 有序矩阵中第 K 小的元素class Solution {#define ll long longpublic: int cal(int mid,vector<vector<int>>&ma){ int n=ma.size(),i=n-1,j=0,res=0; while(i>=0&&j<n){ if(ma[i][j]<=mid) res+=i原创 2021-03-03 14:04:06 · 137 阅读 · 0 评论 -
leetcode 54. 螺旋矩阵
题目class Solution {public: vector<int> spiralOrder(vector<vector<int>>& ma) { if(!ma.size()||!ma[0].size()) return {}; int n=ma.size(),m=ma[0].size(); vector<int>ans; int l=0,r=m-1,up=0,dow原创 2021-03-02 22:46:00 · 45 阅读 · 0 评论 -
leetcode 460. LFU 缓存
题目class LFUCache {public: struct Node{ int key,val,t; Node(int _key,int _val,int _t):key(_key),val(_val),t(_t){} }; typedef list<Node>::iterator It; int mint,capacity,sz; map<int,list<Node>>mp; m原创 2021-03-02 16:35:47 · 63 阅读 · 0 评论 -
面试题 01.07. 旋转矩阵 先折半交换 后对角线交换
题目class Solution {public: void rotate(vector<vector<int>>& a) { int n=a.size(); for(int i=0;i<n/2;++i) for(int j=0;j<n;++j) swap(a[i][j],a[n-i-1][j]); for(int i=0;原创 2021-02-24 01:27:39 · 120 阅读 · 0 评论 -
leetcode 1438. 绝对差不超过限制的最长连续子数组 (尺取+单调队列维护窗口最大最小值 直接秒)
题目参考 剑指 Offer 59 - IIclass Solution {public: #define N 100005 #define INF 0x3f3f3f3f int q1[N],head1=1,tail1=0; int q2[N],head2=1,tail2=0; void push(int x){ while(head1<=tail1&&q1[tail1]<x) --tail1; q1[原创 2021-02-21 00:48:40 · 96 阅读 · 0 评论 -
leetcode 15. 三数之和 / 两数之和的情况种数
题目class Solution {public: vector<vector<int>>ans; void solve(int x,int y,int z){ vector<int>tmp; tmp.push_back(x),tmp.push_back(y),tmp.push_back(z); ans.push_back(tmp); } vector<vector<int&原创 2021-02-20 23:34:14 · 91 阅读 · 0 评论 -
leetcode 995. K 连续位的最小翻转次数 (思维)
题目class Solution {public: int minKBitFlips(vector<int>& a, int K) { int n=a.size(); int q[n+1],head=1,tail=0,ans=0;//队列里面放进入i代表 i~i+k-1有一次翻转 for(int i=0;i<n;++i){ while(head<=tail&&q[head]+原创 2021-02-18 19:21:59 · 74 阅读 · 0 评论 -
leetcode 128. 最长连续序列 (思维?)
class Solution {public: int longestConsecutive(vector<int>& nums) { int n=nums.size(),*vis=new int[n]; map<int,int>mp; for(int i=0;i<n;++i){ if(!mp.count(nums[i])) mp[nums[i]]=i,vis[i]=0; ..原创 2021-02-16 22:37:07 · 59 阅读 · 0 评论 -
leetcode 491. 递增子序列 (找出所有递增子序列 输出 并不能重复 dfs回溯)
题目class Solution {public: int n; vector<vector<int> >ans; vector<int>tmp; void dfs(vector<int>&a,int pos){ map<int,int>mp; for(int i=pos;i<n;++i){ if(!mp[a[i]]&&(!t原创 2021-02-14 21:50:39 · 149 阅读 · 0 评论 -
leetcode 765. 情侣牵手(模拟,从左往右便利,只要不成对就交换)
题目模拟,只要发现右边不是自己的爱人,就让别人和爱人换一下位置。。class Solution {public: int getlove(int x){return (x&1)?(x-1):(x+1);} int minSwapsCouples(vector<int>& a) { int n=a.size(),vis[n],ans=0; for(int i=0;i<n;++i) vis[a[i]]=i;原创 2021-02-14 00:59:19 · 205 阅读 · 0 评论 -
一些简单的几何。。
593. 有效的正方形枚举所有情况,然后四条边相等,且对角线相等不为0. 其实所有情况只有3中哈哈。注意这种情况:[0,0][1,1][0,0][0,1]dis(p1,p2)=dis(p2,p3)=dis(p3,p4)=dis(p4,p1)=2,。。 但不是正方形。class Solution {public: int dis(vector<int>&p1,vector<int>&p2){ return (p1[0]-p2[原创 2021-02-12 15:19:54 · 72 阅读 · 0 评论 -
leetcode 25. K 个一组翻转链表
题目class Solution {public: ListNode* cal(ListNode* head,int K){ ListNode *now=head->next,*nex=now->next,*nexnex;int cnt=0,flag=0; while(nex&&++cnt<K) nexnex=nex->next,nex->next=now,now=nex,nex=nexnex,flag|=(cnt=原创 2021-02-12 13:09:20 · 72 阅读 · 0 评论 -
leetcode 146. LRU 缓存机制
题目Node要存储key,因为要删除tail时,也需要从map里面删除,然后删除的时候需要ket,所以tail节点里面要含有tail对应的key。别忘了 remove(now)时更新相应的head,tail。class LRUCache {public: class Node{ public: Node(int key,int val){this->key=key,this->val=val,this->pre=NULL,this->nex=原创 2021-02-12 01:19:36 · 58 阅读 · 0 评论 -
leetcode 992. K 个不同整数的子数组 (求有多少个区间满足不同数个数为K 简单)
题目算不上尺取吧,看一眼就会了。class Solution {public: int subarraysWithKDistinct(vector<int>& a, int K) { int n=a.size(),vis[n+1],las[n]; memset(vis,0,sizeof vis); vis[a[n-1]]=n-1; for(int i=n-2;i>=0;--i){原创 2021-02-10 17:04:13 · 143 阅读 · 0 评论 -
leetcode 1631. 最小体力消耗路径 (两点之间花费是差的绝对值 求最短路dijkstra)
题目class Solution {public: struct Edge{int to,len,nex;}*edge;int *head,tot=0; void add(int from,int to,int len){ edge[++tot]=(Edge){to,len,head[from]},head[from]=tot; edge[++tot]=(Edge){from,len,head[to]},head[to]=tot; } #原创 2021-02-02 17:12:50 · 108 阅读 · 0 评论 -
链表一些基本操作
leetcode 206. 反转链表题目class Solution {public: ListNode* reverseList(ListNode* head) { if(!head) return head; ListNode *now=head,*nex=head->next,*nexnex; while(nex) nexnex=nex->next,nex->next=now,now=nex,nex=nexnex;原创 2021-02-02 12:34:19 · 104 阅读 · 0 评论 -
leetcode 424. 替换后的最长重复字符(可将任意k个字符替换成任意字符 求最长重复子串 尺取)
题目找到最长的窗口,这个窗口可满足替换k次所有字符相同。这个窗口满足尺取特性。class Solution {public: int cnt[26]; int characterReplacement(string s, int k) { int n=s.size(),ans=0; int i=0,j=0,mx=0; while(j<n){ while(j<n&&(j-i+1-max(原创 2021-02-02 00:35:45 · 213 阅读 · 0 评论 -
leetcode 365. 水壶问题 (两个水杯容量x、y问是否得到z升的水 随便想想就知道了。。。)
题目x+y以上的得不到。假如x>y:x是第一个杯子、y是第二个杯子。我们可以得到x-y、x-2y、x-3y 知道 x-ny。y这个杯子里面装有x-ky(0<k<=n)水,然后 x装满水,把y这个杯子填满。(x-ky<=y)x这个杯子就得到x-(y-(x-ky))=x-y+x-ky=2x-(k+1)y.假如x-ky>y 则 (x-y+x-ky=2x-(k+1)y)>(y+x-y=2x)、所以2x-(k+1)y就不会等于z了,因为z<x+y<2x;原创 2021-01-31 22:28:36 · 1104 阅读 · 1 评论 -
leetcode 581. 最短无序连续子数组 (思维)
题目给几个样例吧,代码就很容易看明白了。[2,6,4,8,10,9,15][1,3,5,4,2][1,3,7,10,5,6,4,2,12]class Solution {#define INF 100001public: int l=-1,r=-1,k=INF; void cal(int i,int val){ if(l==-1) l=i; r=i+1,k=min(k,val); } int findUnsortedSubarr原创 2021-01-31 22:02:48 · 60 阅读 · 0 评论 -
算分期末复习
文章目录dijkstra求最短路(不能有负边)dijkstra求最短路(不能有负边)#include<bits/stdc++.h>using namespace std;const int INF=0x3f3f3f3f,N=1e5+5;struct Edge{int to,len,nex;}edge[N<<1];int head[N],tot=0;void add(int from,int to,int len){ edge[++tot]=(Edge){to,le原创 2021-01-22 11:32:57 · 70 阅读 · 0 评论 -
leetcode 679. 24 点游戏 (每次选出两个数+-*/)
题目比如3*(4+8/2) 就是(8/2+4)*3 每次选出来两个数本来有n个数,每次选出两个数+ - * / 得到一个数,算剩下的n-1个数是否可以算出24class Solution {#define eps 1e-6public: bool isZero(double x){return fabs(x)<=eps;} bool dfs(vector<double>nums){ int n=nums.size(); if原创 2020-12-24 11:19:45 · 135 阅读 · 0 评论 -
leetcode343. 整数拆分 (将n拆成很多正整数 求他们乘积最大 数学证明)
题目class Solution {#define ll long longpublic: int pow(ll a,int x){ ll res=1; while(x){ if(x&1) res*=a; a*=a,x/=2; } return res; } int integerBreak(int n) { if(n==1||n==2)原创 2020-12-24 09:18:46 · 329 阅读 · 0 评论 -
leetcode 376. 摆动序列 (求最长摆动子序列 直接取山峰端点 模拟超简单)
题目模拟模的好,没烦恼,在此嘲笑一下我的憨憨猪。class Solution {public: int wiggleMaxLength(vector<int>& nums) { int n=nums.size(); if(n<2) return n; int ans=1,flag=-1; for(int i=1;i<n;++i){ if(nums[i]==nums[i-1]原创 2020-12-15 22:15:01 · 93 阅读 · 0 评论 -
leetcode 386. 字典序排数 给定 n =1 3,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] 直接模拟10叉树
题目class Solution {public: vector<int> lexicalOrder(int n) { vector<int>ans; int cnt=0,now=1; if(cnt<n) ans.push_back(now),++cnt; while(cnt<n){//已经放入了cnt个 if(now*10<=n) now*=10;原创 2020-12-15 21:43:03 · 99 阅读 · 0 评论 -
leetcode 726. 原子的数量 “K4(ON(SO3)2)2“变为“K4N2O14S4“ (栈模拟)
题目样例好似有毒,不知道是我孤陋寡闻,还是出题人见多识广,竟然还有Be32,,(H),,真牛逼哇。导致右括号后一个不一定是数字,还可能是元素了,导致我每次还要las=1,因为比如(H)O2,2不该入栈的。但我们的代码是遇见右括号就要入栈,所以要las=1,将1入栈,相当于没入。class Solution {typedef pair<string,int> pi;public: string cal(int n){////342得到"342" string r原创 2020-12-15 21:40:37 · 132 阅读 · 0 评论 -
leetcode 31. 下一个排列(倒序找到第一个大于前一个数的数,交换后reverse 巨水)
题目倒叙找到第一个>前一个的数,将前一个数 与这个数下标到n-1中最小的>前一个数的数 交换,将这个数到n=1 reverse即可。class Solution {public: void nextPermutation(vector<int>& nums) { int n=nums.size(); int flag=0; for(int i=n-1;i>=1;--i){ if(nu原创 2020-12-14 22:17:21 · 140 阅读 · 0 评论 -
leetcode 46. 全排列 (dfs回溯枚举所有情况)
题目class Solution {public: int jc(int n){ int res=1; while(n!=1) res*=n,--n; return res; } int n; int vis[15],cnt; void dfs(int pos,vector<vector<int>>& ans){ if(pos==n+1) return;原创 2020-12-14 21:50:51 · 124 阅读 · 0 评论 -
leetcode 354. 俄罗斯套娃信封问题(最长上升子序列模板 仅此记录一下nlogn的方法而已)
题目class Solution {public: int maxEnvelopes(vector<vector<int>>& a) { int n=a.size(),*d=new int[n+1]; if(!n) return 0; sort(a.begin(),a.end(),[](const vector<int>& A,const vector<int>& B){原创 2020-12-14 21:48:20 · 104 阅读 · 0 评论 -
leetcode 10. 正则表达式匹配 44. 通配符匹配 (区间dp)
题目class Solution {public: bool isMatch(string s, string p) { int n=s.size(),m=p.size(); bool **dp=new bool*[n+1]; for(int i=0;i<=n;++i){ dp[i]=new bool[m+1]; for(int j=0;j<=m;++j) dp[i][j]=false;原创 2020-12-14 21:45:36 · 110 阅读 · 1 评论 -
leetcode 37. 解数独(回溯dfs)
题目class Solution {public: int row[9],col[9];//每一行,每一列的状态 vector<vector<int> >a;//(9,vector<int>(9,0)); int dfs(int x,int y){ if(x==9&&y==0) return 1; int vis[10];memset(vis,0,sizeof vis); for原创 2020-12-07 14:53:54 · 82 阅读 · 0 评论 -
leetcode 5619. 最小不兼容性(将大小不超过16的数组分成k组 每组无重复元素 求所有组的最大-最小 的和最小 状压dp)
题目时间复杂度:(2^n) * (n^2)空间复杂度:(2^n) * nclass Solution {#define INF 0x3f3f3f3f#define low(i) ((i)&(-i))public: //当要选每个组的不是一个时: //dp[s|x][x]=min(dp[s][y]+nums[x]-nums[y]); //当要选每个组的第一个时: //dp[s|x][...]=min(dp[s][...]); int get(int原创 2020-12-06 19:20:14 · 334 阅读 · 0 评论 -
leetcode 一些跳跃游戏(一点都不如打游戏快乐┭┮﹏┭┮)
文章目录55. 跳跃游戏45. 跳跃游戏 II55. 跳跃游戏题目class Solution {public: bool canJump(vector<int>& nums) { int n=nums.size(),mx=0; for(int i=0;i<n;++i){ if(nums[i]) mx=max(mx,i+nums[i]); else if(mx==i&&原创 2020-12-06 17:44:01 · 129 阅读 · 0 评论 -
leetcode 1044. 最长重复子串(二分+hash / 后缀数组)
题目听说可以后缀数组,。。可我现在全都忘了,有空补(我自己都不信其实我下面这个代码TLE了,可我很无语很无语,这道题我一直没过,官方题解写成那样还好意思当官方题解,我更无语。气死我了。气死我了。气死我了。气死我了。气死我了。//这个题目错了吧,假如是返回所有的,那返回类型不该是vector<string>吗//我这个解法是返回下标最靠前的重复子串class Solution {#define ll long longpublic: int n,*a;ll mod=1LL&原创 2020-12-05 22:52:45 · 157 阅读 · 0 评论 -
leetcode 994. 腐烂的橘子(bfs模板。。)
题目class Solution {public: struct node{int x,y;}; int d[4][2]={-1,0,0,1,1,0,0,-1}; int n,m; int isok(int nx,int ny){return nx>=0&&nx<n&&ny>=0&&ny<m;} int orangesRotting(vector<vector<int>&g原创 2020-12-05 19:32:33 · 79 阅读 · 0 评论