1.LC300带main代码
#include<iostream> #include<cstring> #include<string> #include<bits/stdc++.h> using namespace std; //return the longest increasing sequence class Solution { public: int lengthOfLIS(vector<int>& num) { vector<int> ans; for(auto a:num) { //lower_bound return iterator(迭代器) so use auto simplify auto n=lower_bound(ans.begin(),ans.end(),a); if(n==ans.end()) ans.push_back(a); else *n=a; } return ans.size(); } }; int main() { vector<int> input; freopen("a.in","r",stdin); int x; while(cin>>x) input.push_back(x); cout<<Solution().lengthOfLIS(input)<<endl; return 0; }
2.LC 5 代码带main 多个括号嵌套时注意不要少括号,比如循环条件少()可能导致{}误判不匹配
#include<iostream> #include<cstring> #include<string> #include<bits/stdc++.h> using namespace std; //return the longest increasing sequence class Solution { public: string longestPalindrome(string s) { if(s.empty()) return ""; int n=s.size(),dp[n][n],left=0,len=1; for(int i=0;i<n;++i) { dp[i][i]=1; for(int j=i;j>=0;--j) { dp[j][i]=(s[i]==s[j]&&(i-j<2||dp[j+1][i-1])); if(dp[j][i]&&(i-j+1>len)) { len=i-j+1; left=j; } } } return s.substr(left,len); } }; int main() { string s; freopen("a.in","r",stdin); cin>>s; //cannot use gets in string ,it should in char[20] array // gets(s); cout<<Solution().longestPalindrome(s)<<endl; return 0; }
愿为天下目,萃聚六路华
浙公网安备 33010602011771号