力扣 最長回文子串 C

2021-09-26 15:19:47 字數 541 閱讀 5025

/*

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

輸入: "babad"

輸出: "bab"

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

輸入: "cbbd"

輸出: "bb"

*/#include using namespace std;

#define max(a,b) (((a) > (b)) ? (a) : (b))

#define min(a,b) (((a) < (b)) ? (a) : (b))

class solution

} return s.substr(start, mlen);//擷取回文子串

}private:

int expendaroundcenter(string s, int left, int right)//計算回文數長度,通用奇、偶子串

return r - l - 1;

}};int main()

力扣 最長回文子串

給定乙個字串 s,找到 s 中最長的回文子串。你可以假設 s 的最大長度為 1000。示例 1 輸入 babad 輸出 bab 注意 aba 也是乙個有效答案。我的第一想法是暴力,然後才是中心擴充套件法 雖然知道應該用動態規劃,但是實現不出來 public string longestpalindr...

最長回文子串 力扣

5種解法 1.最長公共子串 2.暴力法 3.動態規劃 4.中心擴充套件法 5.manacher法 以下記錄大佬題解 演算法 什麼叫回文串?如果乙個字串正著讀和反著讀是一樣的,那它就是回文串。中心擴充套件演算法 我們觀察到回文中心的兩側互為映象。因此,回文可以從它的中心展開,並且只有 2n 1 個這樣...

力扣 05最長回文子串

1.暴力法 include 回文數.hpp include include include using namespace std class solution 左邊 右邊 left right return true public string longestpalindrome string s...