算法—字符串操作

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();
    }
};
相关推荐
洛水水15 分钟前
【力扣100题】39.二叉树的最近公共祖先
算法·leetcode·职场和发展
无敌昊哥战神26 分钟前
【LeetCode 134】加油站:图解指针跳跃与 O(N) 极简贪心,避开 Python 隐藏坑!
c语言·python·算法·leetcode
人道领域30 分钟前
【LeetCode刷题日记】222.极速计算完全二叉树节点数:O(log²n)算法揭秘
java·数据结构·算法·leetcode·深度优先
目黑live +wacyltd35 分钟前
算法备案的实操指南(含截图示例)
人工智能·算法·llm·大模型备案·算法备案
小糯米60135 分钟前
C语言 指针4
c语言·数据结构·算法
洛水水39 分钟前
【力扣100题】36.二叉树展开为链表
算法·leetcode·链表
lwf00616440 分钟前
PNN (Product-based Neural Network) 学习日记
算法·机器学习
ZPC821043 分钟前
YOLO-3D + 双目相机 (RGB + 深度 + 点云) → 3D 位置 + 抓取姿态
人工智能·算法·计算机视觉·机器人
ZPC82101 小时前
YOLOv8-3D(3D 目标检测 + 6D 抓取姿态)
算法·机器人
bubiyoushang8881 小时前
基于 TGLVM 算法的迁移学习分类系统
算法·分类·迁移学习