随笔分类 - leetcode
摘要:``` class Solution { public: vector productExceptSelf(vector& nums) { vector q; int t=1; for(auto i:nums) { q.push_back(t); t*=i; } t=1; for(int i=num
阅读全文
摘要:``` class Solution { public: ListNode* reverse(ListNode* head)//翻转以head为头节点的链表 { if(!head||!head->next) return head; auto a=head,b=head->next; while(b
阅读全文
摘要:# 小根堆 ``` class Solution { public: int findKthLargest(vector& nums, int k) { priority_queue,greater> q; for(auto x:nums) { if(q.size()& nums, int k) {
阅读全文
摘要:``` class Solution { public: bool canFinish(int n, vector>& pre) { if(pre.empty()||pre[0].empty()) return true; vector> g(n,vector(n,false)); for(auto
阅读全文
摘要:``` /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x
阅读全文
摘要:``` class Solution { public: bool st[310][310]; int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0}; int m,n; int numIslands(vector>& g) { int res=0; n=g.size(),m=g
阅读全文
摘要:``` class Solution { public: int majorityElement(vector& nums) { int cnt=1; int res=nums[0]; for(int i=1;i<nums.size();i++) { if(nums[i]==res) cnt++;
阅读全文
摘要:``` /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class
阅读全文
摘要:#使用双栈 ``` class MinStack { public: stack st, min_st; MinStack() { min_st.push(INT_MAX); } void push(int val) { st.push(val); int min_val=min_st.top();
阅读全文
摘要:``` class Solution { public: static const int N=20010; int f[N],g[N]; int maxProduct(vector& nums) { int n=nums.size(); int res=nums[0]; f[0]=g[0]=num
阅读全文
摘要:``` class Solution { public: ListNode* sortList(ListNode* head) { if(!head||!head->next) return head; ListNode* fast=head,*slow=head; while(fast->next
阅读全文
摘要:``` class LRUCache { public: struct node { int key,val; node *l,*r; node(int a,int b) { l=r=NULL; key=a; val=b; } }*L,*R; unordered_map mp;//保存key和节点的
阅读全文
摘要:``` /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class
阅读全文
摘要:#取巧 ``` class Solution { public: const int INF=1e9; bool hasCycle(ListNode *head) { bool res=false; auto p=head; while(p) { if(p->val==INF) { res=true
阅读全文
摘要:``` class Solution { public: int singleNumber(vector& nums) { int res=0; for(auto i:nums) res^=i; return res; } }; ```
阅读全文
摘要:* 为什么这题我都不会,脑袋有点累,状态真差 ``` class Solution { public: int longestConsecutive(vector& nums) { unordered_set s(nums.begin(),nums.end());//记录数字是否出现过 int re
阅读全文
摘要:``` class Solution { public: vector row,anti_diag,col,diag; int res=0; void dfs(int n,int x,int y,int cnt) { if(y==n) y=0,x++; if(x==n) { if(cnt==n) r
阅读全文
摘要:``` class Solution { public: vector> res; vector path; vector anti_diag,col,diag; void dfs(int n,int u) { if(u==n) { res.push_back(path); return; } st
阅读全文
摘要:``` class Solution { public: vector res; void dfs(string s,string path,int idx,int cnt)//枚举下一个逗号填哪 { if(idx>=s.size()) { if(idx==s.size()&&cnt==4) { p
阅读全文
摘要:``` class Solution { public: vector> ans; vector path; void dfs(vector nums,int idx)//选择path的下一个数填什么,从下标idx开始选 { if(path.size()>=2) ans.push_back(path
阅读全文