字符串(最長迴文串&無重複子串)

一、最長無重複子串

Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: "abcabcbb"
Output: 3 
Explanation: The answer is "abc", with the length of 3. 

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

分析:本題使用滑動窗口,利用map數據結構,頭指針不停前移,如果頭指針指向的當前字符已經存在於map中,那麼尾指針取當前位置於該字符位置的最大值(尾指針看情況前移),之後更新ans並將當前字符納入map。

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        map<char,int> hash;
        int ans=0;

        //map.find通過key找value
        for(int i=0,j=0;j<s.size();j++)
        {
            if(hash.find(s[i])!=hash.end())//找到了
            {
               i = max(hash.find(s[j])->second,i);
            }
            ans = max(ans,j-i+1);
            hash[s[j]] = j+1;
        }
        
        
        return ans;
    }
}; 

二、最長迴文串

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:

Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"
Output: "bb"

分析:

1.可使用dp,定義狀態i,j爲字符串i到j是否爲迴文串

P(i,j)=(P(i+1,j−1) and Si==Sj) 

The base cases are:

P(i,i)=true P(i, i) = true P(i,i)=true

class Solution {
public:
    string longestPalindrome(string s) {
        int l = s.size();
        if(l==0)
            return "";
        bool dp[l][l] ;
        for(int i=0;i<l;i++)
            for(int j=0;j<l;j++)
                dp[i][j]=false;
        int mi=0, mj=1;
        for(int i=0;i<l;i++)
        {
            dp[i][i] = true;
            if(i != l-1 )
                if(s[i]==s[i+1])
                {
                    dp[i][i+1]=true;
                    mi=i;
                    mj=2;
                }
            
        }
        
        for(int d=2;d<l;d++)
            for(int i=0;i<l-d;i++)
            {
                int j=i+d;
                if(dp[i+1][j-1] && s[i]==s[j])
                {
                    dp[i][j]=true;
                    mi=i;
                    mj=d+1;
                }
                
            }
        cout<<mi<<mj;
        return s.substr(mi,mj);//mi爲起始位置,j爲長度
    }
};

2.可以使用從中心擴展法

分析:迴文串最多有2n-1箇中心,我們遍歷這些中心,尋找最長串

class Solution {
public:
    string longestPalindrome(string s) {
        int l = s.size();
        if(l==0)
            return "";

        int start = 0;
        int len;
        for(int i=0;i<l-1;i++)
        {
            int len1 = expend(s,i,i);
            int len2 = expend(s,i,i+1);
            if(max(len1,len2)>len)
            {
                len = max(len1,len2);
                start = i - (len-1)/2 ;
            }
            cout<<start<<' '<<len<<endl;
        }
        
        return s.substr(start,len);
    }
    int expend(string s, int L,int R)
    {
        while(L>=0 && R<s.size() && s[L]==s[R])
        {
            L--;
            R++;
        }
        return R-L-1;
    }
};


 

 

 

 

 

 

 

 

 

發佈了9 篇原創文章 · 獲贊 26 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章