The sword refers to the offer's 10th day dynamic programming (moderate)

Sword to Offer 46. Translate numbers into strings

topic ideas

It is to judge whether two adjacent numbers can form a number, and then use the dp relationship to infer

code

class Solution {
    
    
private:
    int dp[100];
    string s;
public:
    int translateNum(int num) {
    
    
        s = to_string(num);
        int n=s.length();
        dp[0]=1;
        dp[1]=1;
        for(int i=2;i<=n;i++){
    
    
            int t=(s[i-2]-'0')*10+s[i-1]-'0';
            if(t>=10&&t<=25){
    
    
                dp[i]=dp[i-1]+dp[i-2];
            }else{
    
    
                dp[i]=dp[i-1];
            }
            //cout<<dp[i]<<" ";
        }
        //cout<<s<<endl;
        return dp[n];
    }
};

Sword refers to Offer 48. The longest substring without repeating characters

topic ideas

This can be done with a queue, refer to the topic of sliding window, continue to add the number and update the result

code

class Solution {
    
    
public:
    int lengthOfLongestSubstring(string s) {
    
    
        int n=s.length();
        int l=0,r=0;
        int vis[200]={
    
    0};
        char s1[40010];
        int len=0;
        for(int i=0;i<n;i++){
    
    
            //cout<<i<<" "<<s[i]<<" "<<vis[s[i]]<<endl;
            if(!vis[s[i]]){
    
    
                s1[r]=s[i];
                vis[s[i]]=1;
                r++;
            }else{
    
    
                while(l<r&&vis[s[i]]){
    
    
                    vis[s1[l]]=0;
                    l++;
                }
                s1[r]=s[i];
                vis[s[i]]=1;
                r++;
            }
            len = max((r-l),len);
        }
        return len;
    }
};

If this article is helpful to my friends, I hope to give a like and support~ Thank you very much~

insert image description here


Guess you like

Origin blog.csdn.net/weixin_46627433/article/details/123100610