練習10 最長回文子串(LeetCode)

2021-09-13 10:44:04 字數 867 閱讀 4728

給定乙個字串s,找到s中最長的回文子串。你可以假設s的最大長度為 1000。

示例 1:

輸入: "babad"

輸出: "bab"

注意: "aba" 也是乙個有效答案。

示例 2:

輸入: "cbbd"

輸出: "bb"

首先我直接採用了最暴力的解法,從頭到尾依次比較,時間複雜度為o(n^3),**如下:

string longestpalindrome(string s) }}

return s.substr(start,maxlength);

}

系統不通過,執行時間超時。

然後學習了動態規劃演算法,**如下:

string longestpalindrome(string s) 

inputfile.close();

for(int i=0;i<=peopletotal;i++)

else

}else if(people>=peopleneed[minenum])

else

maxgold[people][minenum]=retmaxgold;

return retmaxgold;

}int main()

leetcode練習 最長回文子串

給定乙個字串 s,找到 s 中最長的回文子串。你可以假設 s 的最大長度為 1000。示例一 輸入 babad 輸出 bab 注意 aba 也是乙個有效答案 示例二 輸入 cbbd 輸出 bb 通過substring劃分子串,遍歷所有可能結果。結果效率太低 如圖 class solution ret...

最長回文子串 最長回文子串行

1.最長回文子串行 可以不連續 include include include include using namespace std 遞迴方法,求解最長回文子串行 intlps char str,int i,int j intmain include include include using n...

lintcode練習 200 最長回文子串

給出乙個字串 假設長度最長為1000 求出它的最長回文子串,你可以假定只有乙個滿足條件的最長回文串。給出字串 abcdzdcab 它的最長回文子串為 cdzdc o n2 時間複雜度的演算法是可以接受的,如果你能用 o n 的演算法那自然更好。class solution param s input...