笔面试算法--字符串专题(反转字符串、进制转换、大数加法、最长回文子串、kmp算法)

这篇博客涵盖了多种字符串操作的算法实现,包括字符串反转、进制转换、数字相加以及最长回文子串查找。此外,还介绍了KMP算法在字符串匹配中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class Solution {
public:
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    string solve(string str) {
        // write code here
        string t="";
        for(int i=str.size()-1;i>=0;i--)
            t+=str[i];
        return t;
    }
};

class Solution {
public:
    /**
     * 进制转换
     * @param M int整型 给定整数
     * @param N int整型 转换到的进制
     * @return string字符串
     */
    string solve(int M, int N) {
        // write code here
        int ls=abs(M);
        string s;
        stack<char>st;
        while(ls)
        {
            if(ls%N<10)
                st.push(ls%N+'0');
            else if(ls%N==10)
                st.push('A');
            else if(ls%N==11)
                st.push('B');
            else if(ls%N==12)
                st.push('C');
            else if(ls%N==13)
                st.push('D');
            else if(ls%N==14)
                st.push('E');
            else if(ls%N==15)
                st.push('F');
            ls/=N;
        }
        if(M<0)
            st.push('-');
        while(!st.empty())
        {
            s+=st.top();
            st.pop();
        }
        return s;
    }
};

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 计算两个数之和
     * @param s string字符串 表示第一个整数
     * @param t string字符串 表示第二个整数
     * @return string字符串
     */
    string solve(string s, string t) {
        // write code here
        int cd1=s.size();
        int cd2=t.size();
        string ans;
        while(cd1<cd2)
        {
            s="0"+s;
            cd1++;
        }
        while(cd1>cd2)
        {
            t="0"+t;
            cd2++;
        }
//        cout<<t<<endl;
        int C=0;
        for(int i=cd1-1; i>=0; i--)
        {
            int tmp=(s[i]-'0'+t[i]-'0'+C);
            ans+=char(tmp%10+'0');
            C=tmp/10;
        }
        if(C)
            ans+=(C+'0');
        reverse(ans.begin(),ans.end());
        return ans;
    }
};

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param A string字符串 
     * @return int整型
     */
    int getLongestPalindrome(string A) {
        // write code here
        int n=A.size();
        string s="#";
        for(int i=0;i<n;i++){
            s+=A[i];
            s+="#";
        }
        int ans=0;
        int R=0;
        int L=0;
        int mid=0;
        vector<int>Len(s.length());
        for(int i=0;i<s.length();i++){
            if(i>=R){
                int len=0;
                while((i-len>=0&&i+len<=s.length())&&s[i-len]==s[i+len])
                    len++;
                Len[i]=len-1;
            }
            else{
                int i_=mid-(i-mid);
                int L_=i_-Len[i_];
                if(L_>L)//在边界内
                    Len[i]=Len[i_];
                else if(L_<L)//在边界外
                    Len[i]=R-i;
                else{
                    int len=R-i+1;
                    while((i-len>=0&&i+len<=s.length())&&s[i-len]==s[i+len])
                        len++;
                    Len[i]=len-1;
                }
            }
            if(i+Len[i]>R){
                mid=i;
                R=i+Len[i];
                L=i-Len[i];
            }
            ans=max(ans,Len[i]*2+1);
        }
        return ans/2;
    }
};

 

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 计算模板串S在文本串T中出现了多少次
     * @param S string字符串 模板串
     * @param T string字符串 文本串
     * @return int整型
     */
    int kmp(string S, string T) {
        // write code here
        vector<int> next(S.length()+1);
        next[0]=next[1]=0;
        for(int i=1;i<S.length();i++){
            int j=next[i];
            while(S[j]!=S[i]&&j!=0)
                j=next[j];
            if(S[j]==S[i])
                next[i+1]=j+1;
            else
                next[i+1]=0;
        }
        int j=0;
        int ans=0;
        for(int i=0;i<T.length();i++){
            while(T[i]!=S[j]&&j!=0)
                j=next[j];
            if(T[i]==S[j])
                j++;
            if(j==S.length()){
                ans++;
                j=next[j];
            }
        }
        return ans;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值