随笔分类 - leetcode
摘要:思路 暴力就是枚举终点 i,找出里 i 最近的起点 j,再去更新答案,可以发现起点随终点单调往后,因此可以滑动窗口优化 如何快速判断当前窗口是否包含子串所有字符 哈希表 word 存储子串所有字符出现的次数,window 存储当前窗口所有字符出现的次数 变量 cnt 记录当前窗口里,有效字符的个数
阅读全文
摘要:class Solution { public: bool isScramble(string s1, string s2) { int n=s1.size(); vector<vector<vector<bool> > >f(n,vector<vector<bool>>(n,vector<bool
阅读全文
摘要:class Solution { public: int maxProfit(vector<int>& prices) { int res=0; for(int i=0;i<prices.size()-1;i++) res+=max(0,prices[i+1]-prices[i]); return
阅读全文
摘要:class Solution { public: int maxProfit(vector<int>& prices) { int buy=prices[0],n=prices.size(),res=0;//记录最小值 for(int i=1;i<n;i++)//枚举第几天卖出 { res=max(
阅读全文
摘要:class Solution { public: int f[110],g[110];//分别表示第i个房屋偷,不偷的最大价值 int rob(vector<int>& nums) { int n=nums.size(); for(int i=1;i<=n;i++) { g[i]=max(f[i-1
阅读全文
摘要:class Solution { public: int f[1010][1010];//f[i][j]表示s[i~j]之间的最长序列 int INF=0x3f3f3f3f; int longestPalindromeSubseq(string s) { int n=s.size(); s=' '+
阅读全文
摘要:class Solution { public: int f[1010][1010];//f[i][j]表示s1的下标i结尾,s2下标j结尾的最长公共子数组长度 int findLength(vector<int>& nums1, vector<int>& nums2) { int n=nums1.
阅读全文
摘要:思路 任何时刻,某个石头的重量永远都是若干石头加减运算的绝对值 如 a-b+c 合并石头都是减法,但仍可能出现+运算符,如 a-(b-c)=a-b+c 任何一种合并方法,最后一个石头的重量都可以表示成一种代数形式,如 a+b-c+d+e+f-g 不是所有的代数形式都可以转换为一种合并方法,如 a+b
阅读全文
摘要:class Solution { public: int f[510][510];//f[i][j]表示将s1前i个字符和s2前j个字符的最长公共子序列长度 int minDistance(string word1, string word2) { int n=word1.size(),m=word
阅读全文
摘要:class Solution { public: int f[60];//f[i]记录i能拆出的最大乘积 int integerBreak(int n) { for(int i=2;i<=n;i++) for(int j=1;j<i;j++)//枚举最后一个拆出的数字,这里不能只循环到i/2 f[i
阅读全文
摘要:class Solution { public: long long f[1010];//f[i]表示总和为i的选法个数 int combinationSum4(vector<int>& nums, int target) { int n=nums.size(); f[0]=1; for(int i
阅读全文
摘要:class Solution { public: bool f[310]; bool wordBreak(string s, vector<string>& wordDict) { unordered_set<string> hashtable; for(auto i:wordDict) hasht
阅读全文
摘要:class Solution { public: bool check(string s) { int n=s.size(); for(int i=0;i<n/2;i++) if(s[i]!=s[n-i-1]) return false; return true; } vector<vector<s
阅读全文
摘要:class Solution { public: long long f[1010][1010];//f[i][j]表示s前i个字符得到t前j个字符的所有方案 int numDistinct(string s, string t) { f[0][0]=1; int n=s.size(),m=t.si
阅读全文
摘要:class Solution { public: int f[25][2010];//体积范围从-1000~1000 int findTargetSumWays(vector<int>& nums, int target) { int n=nums.size(),offset=1000;//价值总和
阅读全文
摘要:class Solution { public: bool f[110][110]; bool isInterleave(string s1, string s2, string s3) { int n=s1.size(),m=s2.size(); if(n+m!=s3.size()) return
阅读全文
摘要:class Solution { public: int f[25];//f[i]表示i个数可以构成的树的个数 int numTrees(int n) { f[0]=1; for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++)//以j为根节点 f[i]+=f[j-
阅读全文
摘要:class Solution { public: int f[110]; bool check(char a,char b) { if(a>='1'&&a<='9'&&b>='0'&&b<='9') { int c=a-'0'; c=c*10+(b-'0'); if(c>=1&&c<=26) ret
阅读全文
摘要:/* // Definition for a Node. class Node { public: int val; Node* next; Node* random; Node(int _val) { val = _val; next = NULL; random = NULL; } }; */
阅读全文
摘要:class Solution { public: int method(vector<int> h)//求柱状图中最大的矩形 { int n=h.size(); vector<int> l=vector<int> (n),r=l; stack<int> st; //预处理l,r数组 for(int
阅读全文