随笔分类 - leetcode
摘要:``` class Solution { public: int res=0; int lengthOfLongestSubstring(string s) { int n=s.size (); if(!n) return 0; bool st[128]={false}; for(int j=0,i
阅读全文
摘要:``` class Solution { public: vector dailyTemperatures(vector& t) { stack st; int n=t.size(); vector res(n); for(int i=n-1;i>=0;i--) { while(st.size()&
阅读全文
摘要:# 暴力 ``` class Solution { public: int res=0; int countSubstrings(string s) { int n=s.size(); for(int i=0;i=0&&r=0&&r> f(n+1,vector(n+1,false)); for(in
阅读全文
摘要:``` class Solution { public: int findUnsortedSubarray(vector& nums) { int n=nums.size(); int l=0,r=n-1; while(l0&&nums[r]>=nums[r-1]) r--; int min_num
阅读全文
摘要:``` /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr),
阅读全文
摘要:``` class Solution { public: int hammingDistance(int x, int y) { int cnt=0; int t=x^y; while(t) { cnt+=(t&1); t>>=1; } return cnt; } }; ```
阅读全文
摘要:``` class Solution { public: vector findDisappearedNumbers(vector& nums) { vector res; int n=nums.size(),offset=n+1; for(auto &num:nums) { int x=(num)
阅读全文
摘要:``` class Solution { public: vector findAnagrams(string s, string p) { unordered_map window,hash; vector res; for(auto i:p) hash[i]++; for(int i=0,j=0
阅读全文
摘要:``` class Solution { public: int subarraySum(vector& nums, int k) { int n=nums.size(),res=0; vector s(n+1,0); unordered_map hash;//记录端点i之前所有前缀和的出现情况 f
阅读全文
摘要:``` /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr),
阅读全文
摘要:``` class Solution { public: struct node { int val; int pre; node* next; node(int a,int b,node* c) { val=a; pre=b; next=c; } }; void insert(node* &hea
阅读全文
摘要:``` class Solution { public: vector calcEquation(vector>& equations, vector& values, vector>& queries) { unordered_set node;//记录所有节点 unordered_map> g;
阅读全文
摘要:``` class Solution { public: string dfs(string s,int &idx) { string str; while(idx'0'&&s[idx]='0'&&s[idx]<='9') num+=s[idx++]; if(s[idx]=='[') { int c
阅读全文
摘要:# 快排思想 - 注意,这里是倒序排序,因此应该`while(nums[i].cnt>x);` ``` class Solution { public: struct element { int val,cnt; element(int a,int b) { val=a; cnt=b; } }; v
阅读全文
摘要:``` class Solution { public: vector countBits(int n) { vector f(n+1); for(int i=1;i>1]+(i&1); return f; } }; ```
阅读全文
摘要:## 思路 本题有两点需要解决,如果确定括号的最少删除数量以及删除哪些括号 #### 求出最少删除数量 思路见 LeetCode 22. 括号生成 #### 已知删除数量,删除哪些括号? 直接爆搜,枚举每个括号是否删除 - 如果当前是左括号,枚举是否删除 (删除 0 还是 1 个) - 如果当前是左
阅读全文
摘要:``` class Solution { public: int findDuplicate(vector& nums) { if(nums.size()<2) return nums[0]; int n=nums.size(); int fast=0,slow=0; do { slow=nums[
阅读全文
摘要:``` class Solution { public: void moveZeroes(vector& nums) { if(nums.empty()) return; int n=nums.size(); int idx=n-1; while(idx>=0&&nums[idx]==0) idx-
阅读全文
摘要:``` class Solution { public: bool searchMatrix(vector>& matrix, int target) { if(matrix.empty()||matrix[0].empty()) return false; int n=matrix.size(),
阅读全文
摘要:``` class Solution { public: vector maxSlidingWindow(vector& nums, int k) { deque q; vector res; for(int i=0;i=k) q.pop_front(); //插入新元素 while(q.size(
阅读全文