LeetCode5:Longest Palindromic Substring
題目:
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
解題思路:
主要有三種:
第一種:Manacher演算法,也是最快的,時間複雜度為O(n)
第二種:DP演算法,時間複雜度為O(n*n)
第三種:中心法,時間複雜度為O(n*n)
實現程式碼:
#include <iostream> #include<vector> using namespace std; /** Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. */ class Solution { public: //Manacher演算法(O(n)) string longestPalindrome(strings) { string p; if(s.empty()) return p; int id = 0; int mx = 0; //以下對要操作的副本str進行格式化,使得其為奇數 string str("^"); for(int i = 0; i < s.size(); i++) { str += "#"; str += s[i]; } str+= "#$"; vector<int> r(str.size(), 0); for(int i = 1; i < str.size()-1; i++) { if(mx > i) r[i] = min(r[2*id - i], mx - i); else r[i] = 1; while(str[i+r[i]] == str[i-r[i]])//為了防止越界,str頭尾插入了'^'和'$'字元 r[i]++; if(r[i] + i > mx) { mx = r[i] + i; id = i; } } int maxlen = 0; int maxid = 0; for(int i = 1; i < str.size()-1; i++) { if(r[i] > maxlen) { maxlen = r[i]; maxid = i; } } //(maxid-1)/2為原字串中最長迴文字串中心字元位置 //(maxlen-1)/2為原字串中最長迴文子串半徑 //maxlen-1為原字串中最長迴文字串長度 return s.substr((maxid-1)/2 - (maxlen-1)/2, maxlen-1); } //DP O(n*n) string longestPalindrome2(string s) { string p; if(s.empty()) return p; int len = s.size(); vector<vector<bool>> dp(len, vector<bool>(len, false));//dp[i][j]表示i~j的字串是否為迴文串 int maxstart = 0; int maxlen = 1; for(int i = 0; i < len-1; i++) { dp[i][i] = true; if(s[i] == s[i+1]) { dp[i][i+1] = true; maxstart = i; maxlen = 2; } } for(int l = 3; l <= len; l++) { for(int i = 0; i < len-l+1; i++) { int j = i+l-1; if(s[i] == s[j] && dp[i+1][j-1]) { dp[i][j] = true; maxstart = i; maxlen = l; } } } return s.substr(maxstart, maxlen); } //中心法,以每一個字元作為迴文串中心,向兩邊擴充套件 string longestPalindrome3(string s) { string p; if(s.empty()) return p; int len = s.size(); int maxstart = 0; int maxlen = 0; for(int i = 0; i < len; i++) { int l = i-1; int r = i+1; int tmpmax = 1;//已i為中心的迴文串:奇數 while(l >= 0 && r < len && s[l--] == s[r++]) tmpmax++; if(maxlen < tmpmax*2 -1) { maxlen = tmpmax*2 -1; maxstart = l+1; } int l2 = i; int r2 = i+1; int tmpmax2 = 0;//已i和i+1為中心的迴文串,偶數時 while(l2 >= 0 && r2 < len && s[l2--] == s[r2++]) tmpmax2++; if(maxlen < tmpmax2*2) { maxlen = tmpmax2*2; maxstart = l2+1; } } return s.substr(maxstart, maxlen); } }; int main(void) { string s("abbacdd"); Solution solution; string p = solution.longestPalindrome3(s); cout<<p<<endl; return 0; }
相關推薦
leetcode5:Longest Palindromic Substring最長迴文子串
python版: class Solution: def longestPalindrome(self, s): """ :type s: str :rtype: str """ start = en
LeetCode5:Longest Palindromic Substring
題目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longes
【LeetCode & 劍指offer刷題】字串題13:Longest Palindromic Substring
【LeetCode & 劍指offer 刷題筆記】目錄(持續更新中...) Longest Palindromic Substring Given a string s , find the longest
LeetCode-005: Longest Palindromic Substring
題目: 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: "bab
刷題筆記:leetcode第5題:Longest Palindromic Substring
記錄一下~~ 作者刷leetcode刷到吐血,第五題感覺沒有太好的官方解決方法,我是這麼做的 問題: 思路:由於字串可能很大,消耗計算量的做法肯定是不能用的,最後只遍歷一次,找出所有迴文 1、遍歷每個字元 2、每次得到字元後,再迴圈
Leetcode5 Longest Palindromic Substring
太久不做題了…這道題做了好久。 這是一道Tag是DP的題。於是自己開始就按DP的思路去做了。但是最後發現貌似並不是典型DP,而是DP+遞迴+雙指標(?),個人菜雞感覺。 看了Solution,總結下思考過程。 首先暴力解法,相當於在一個長度為n的字串裡選擇兩個數分別作為字串的開始和結尾,然後對這個子字串
LeetCode:5. Longest Palindromic Substring(找出一個字串中最大的子迴文串)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of sis 1000. Example 1: Input
Leetcode#5. Longest Palindromic Substring(最長迴文子串:二種解法)
宣告:題目解法使用c++和Python兩種,重點側重在於解題思路和如何將c++程式碼轉換為python程式碼。 本題c++採用兩種方法解答,python用到了閉包的知識。 題目 Given a string s, find the longest pali
【LeetCode】Longest Palindromic Substring && 【九度】題目1528:最長迴文子串(騰訊2013年實習生招聘二面面試題)
Longest Palindromic Substring Total Accepted: 4808 Total Submissions: 23151 My Submissions Given a string S, fi
Python 刷題日記:LeetCode 5&9&516- Longest Palindromic Substring 題集合
原題:LeetCode 5 Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You may assume that
演算法設計與分析(5)-- Longest Palindromic Substring(難度:Medium)
演算法設計與分析(5) 題目:Longest Palindromic Substring(最長的迴文字元子串) 問題描述:Given a string s, find the longe
最長迴文:LeetCode:5. Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exist
LeetCode ---Longest Palindromic Substring
ray leetcode clas [] urn char har () rar 1 public String longestPalindrome(String s) { 2 char[] ch = s.toCharArray(); 3
Java Longest Palindromic Substring(最長回文字符串)
post 復雜 sam check eth println void empty [] 假設一個字符串從左向右寫和從右向左寫是一樣的,這種字符串就叫做palindromic string。如aba,或者abba。本題是這種,給定輸入一個字符串。要求輸出一個子串,使得子串
[Leetcode] Longest palindromic substring 最長回文子串
最長回文子串 lee ++ string lin find bsp 解決方法 相同 Given a string S, find the longest palindromic substring in S. You may assume that the maximum
LeetCode - 5 - Longest Palindromic Substring
ems nbsp 偶數 運行時 ges style string -1 lee 題目 URL:https://leetcode.com/problems/longest-palindromic-substring 解法 一、循環搜索 對於每一個字符,往後搜索,遇到相同
Leetcode 5. Longest Palindromic Substring
int style valid sum nbsp next htm targe 回文 Given a string s, find the longest palindromic substring in s. You may assume that the maximum
Leet code problem 5 Longest Palindromic Substring
return div color rom c-s stp ble -- == class Solution { public: string longestPalindrome(string s) { int x = -1;
Leetcode 5: Longest Palindromic Substring
amp input tmp esc -- solution pan else bst Given a string s, find the longest palindromic substring in s. You may assume that the maximu
5. Longest Palindromic Substring
put fin pan col solution spa you subst nbsp Given a string s, find the longest palindromic substring in s. You may assume that the maximu