算法—字符串操作

394. 字符串解码 - 力扣(LeetCode)

cpp 复制代码
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)

cpp 复制代码
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)

cpp 复制代码
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)

cpp 复制代码
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)

cpp 复制代码
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();
    }
};
相关推荐
AICDragon4 分钟前
1.8%就够了:K3的896个MoE专家,为什么激活率这么低反而是好事?
人工智能·算法
VALENIAN瓦伦尼安教学设备23 分钟前
ASHOOTER激光对中仪如何通过颜色确定调整是否合适
数据库·嵌入式硬件·算法
货拉拉技术24 分钟前
重塑 Agent 度量衡:基于 LLM-as-a-Judge 的离线评估体系与实践
算法·设计模式
车压37 分钟前
从“为什么”理解注意力机制
算法
wabs6661 小时前
关于哈希表【力扣15.三数之和的思考】
数据结构·算法·leetcode·散列表·哈希表·三数之和
退休倒计时1 小时前
【每日一题】LeetCode 88. 合并两个有序数组 TypeScript
算法·leetcode·职场和发展·typescript
键盘会跳舞1 小时前
C++:函数对象与 std::function 源码级深度拆解——泛型算法的策略内核与可调用对象统一封装
c++·算法·仿函数
手写码匠1 小时前
华为云Flexus+DeepSeek征文|Agent 评测体系实战:用 DeepSeek-R1 当裁判,打造 Dify Agent 的自动化回归测试流水线
人工智能·深度学习·算法·aigc
LONGZETECH1 小时前
无人机实训高成本痛点解法:虚拟仿真实现 70% 耗材损耗下降
大数据·算法·unity·架构·无人机
不会就选b1 小时前
算法日常・每日刷题--<队列,宽搜>4
算法