394. 字符串解码 - 力扣(LeetCode)
class Solution {
public:string longestCommonPrefix(vector<string>& strs) { string ret=strs[0];//***1***记得先要初始化ret,作为第一个比较值for(int i=0;i<strs.size();i++){ret=foundcommon(ret,strs[i]); }return ret;}string foundcommon(string str1,string str2){int len_min=min(str1.size(),str1.size());int i=0;for(i=0;i<len_min;i++){if(str1[i]!=str2[i]) break;}return str1.substr(0,i);}
};
. - 力扣(LeetCode)
class Solution {
public:string longestPalindrome(string s) {//枚举所有子串O(n^2),回文子串的特点,中心轴对称,int begin=0; int end=0; int n=s.size();int len=0;for(int i=0;i<n;i++){int left=i; int right=i;while(left>=0&&right<n&&s[left]==s[right]){left--;right++;}if(right-left+1>len){len=right-left+1;begin=left;end=right;}}for(int i=0;i<n;i++){int left=i; int right=i+1;while(left>=0&&right<n&&s[left]==s[right]){left--;right++;}if(right-left+1>len){len=right-left+1;begin=left;end=right;}}return s.substr(begin,len);}
};
https://leetcode.cn/problems/add-binary/submissions/524491385/https://leetcode.cn/problems/add-binary/submissions/524491385/
67. 二进制求和 - 力扣(LeetCode)
class Solution {
public:string addBinary(string a, string b) {string ret;int cur1=a.size()-1;int cur2=b.size()-1;int t=0;//保留进位while(cur1>=0||cur2>=0||t){if(cur1>=0) t+=a[cur1--]-'0';if(cur2>=0) t+=b[cur2--]-'0';ret+=t%2+'0';//低位在left,需要反转t/=2;//进位}reverse(ret.begin(),ret.end());return ret;}};
43. 字符串相乘 - 力扣(LeetCode)
class Solution {
public:string multiply(string num1, string num2) {int m=num1.size();int n=num2.size();reverse(num1.begin(),num1.end());reverse(num2.begin(),num2.end());vector<int> tmp(m+n);//实际只用m+n-1个位数,也就是0~m+n-2,对于无进位相加。//先无进位相乘cout<<m<<" "<<n<<endl;for(int i=0;i<m;i++){for(int j=0;j<n;j++){tmp[i+j]+=(num1[i]-'0')*(num2[j]-'0');//**1**+=不是=,这里是累加cout<<tmp[i+j]<<endl;}}for(int i=0;i<m+n-1;i++){cout<<tmp[i]<<endl;}//处理进位string ret;int t=0;int i=0;while(i<m+n-1||t){//**3**i<m+n-1:避免ret什么都不放,不用tmp[i]!=0:可能有问题,111,100;直接结束循环t+=tmp[i++];ret+=t%10+'0';t/=10;}//处理ret最后插入的0,因为是倒序,所以无法此0是前导0,不能统计while(ret.size()>1&&ret.back()=='0') ret.pop_back();//**2**size()=1时候,只有一位,此位为0,所以不为前导0.reverse(ret.begin(),ret.end());return ret;}
};
394. 字符串解码 - 力扣(LeetCode)
class Solution {
public:string decodeString(string s) {stack<int> nums;stack<string> st;st.push("");int i=0; int n=s.size();while(i<n){if('0'<=s[i]&&s[i]<='9'){int tmp=s[i++]-'0';cout<<"tmp:"<<tmp<<endl;cout<<s[i]<<endl;while('0'<=s[i]&&s[i]<='9') {tmp=tmp*10+s[i++]-'0';//***1**提取数组:不是+=,通过变换也不行,是=cout<<tmp<<endl;}nums.push(tmp);cout<<"nums栈顶"<<nums.top()<<endl;//cout<<"st栈顶"<<st.top()<<endl;}else if(s[i]=='['){cout<<"进入"<<endl;i++;//提取字母string str1="";cout<<s[i]<<endl;while('a'<=s[i]&&s[i]<='z'){//牢记提取一串子数组的话,判断条件两个必须分开写str1+=s[i++];cout<<str1<<endl;}// cout<<str1<<endl;st.push(str1);// cout<<"nums栈顶"<<nums.top()<<endl;cout<<"st栈顶"<<st.top()<<endl;}else if(s[i]==']'){//提取最内层的字符串int k=nums.top();nums.pop();string str1=st.top();st.pop();string str2="";while(k--){str2+=str1;}st.top()+=str2;i++;//cout<<"nums栈顶"<<nums.top()<<endl;// cout<<"st栈顶"<<st.top()<<endl;}else{while('a'<=s[i]&&s[i]<='z') st.top()+=s[i++];// cout<<"nums栈顶"<<nums.top()<<endl;// cout<<"st栈顶"<<st.top()<<endl;}}return st.top();}
};