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.
Example:
Input: “babad”
Output: “bab”
Note: “aba” is also a valid answer.
Example:
Input: “cbbd”
Output: “bb”
思路
吃完飯難得和某爽約演算法題,看到題目是最大回文子串,emmmm沒做過,唯一有點印象就是大一學程式設計的時候做過判斷是否是迴文串的問題。判斷不出這道題用不用動規,索性暴力,,,
總體思路是這樣的:
從每個位置判斷以該位置為中心點的時候最長迴文子串的長度。
- 如果該位置的相鄰字元均與之不同,直接往左右兩個方向搜尋直到不是迴文串或者越界。
- 如果該位置的相鄰字元與之相同,則進行整合,把相同的字元一整串看成一體,確定左邊最小的座標與右邊最大的座標,然後往左右兩個方向搜尋直到不是迴文串或者越界。
程式碼
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
string longestPalindrome(string s) {
string lps = "";
int maxL=0; //記錄最大回文子串長度
for(int i = 0;i < s.length();i++){
int left = i, right = i;
while(i+1 < s.length() && s[i]==s[i+1]){
right = i+1; //如果s[i]==s[i+1],更新right,即相同字元右邊最大的座標
i++;
}
while(left>=0 && right<s.length() && s[left] == s[right]){
left--;right++; //判斷不是迴文串的情況
}
if(right-left-1 > maxL){
lps = s.substr(left+1, right-left-1); //提取當前最大回文子串
maxL = right-left-1;
}
}
return lps;
}
};
int main()
{
string temp;
cin >> temp;
Solution s = Solution();
cout << s.longestPalindrome(temp);
return 0;
}
想不到這種暴力做法居然過了,,,而且run time 6ms,,,寫完blog某爽還沒打完哈哈哈哈一包辣條到手Yeah! 時間複雜度O(n^2)的樣子Orz
相關文章
- Leetcode Longest Palindromic SubstringLeetCode
- LeetCode 5 (Longest Palindromic Substring)LeetCode
- Leetcode-Longest Palindromic SubstringLeetCode
- Longest Palindromic Substring leetcode javaLeetCodeJava
- [LeetCode] 5. Longest Palindromic SLeetCode
- LeetCode OJ : 5 Longest Palindromic SubstringLeetCode
- LeetCode每日一題:longest palindromic substringLeetCode每日一題
- [LeetCode] Longest Palindromic Substring 最長迴文子串LeetCode
- Leetcode5: Longest Palindromic Substring(最長迴文子串)LeetCode
- Leetcode Longest Substring Without Repeating CharactersLeetCode
- Leetcode 3 Longest Substring Without Repeating CharactersLeetCode
- Leetcode-Longest Substring Without Repeating CharactersLeetCode
- Longest Substring Without Repeating Characters leetcode javaLeetCodeJava
- Leetcode 3. Longest Substring Without Repeating CharactersLeetCode
- LeetCode-Longest Substring with At Least K Repeating CharactersLeetCodeAST
- LeetCode-Longest Substring with At Most K Distinct CharactersLeetCode
- LeetCode OJ : 3 Longest Substring Without Repeating CharactersLeetCode
- Leetcode javascript 3 longest-substring-without-repeating-charactersLeetCodeJavaScript
- Leetcode-Longest Substring with At Most Two Distinct Characters.LeetCode
- [LeetCode] 2414. Length of the Longest Alphabetical Continuous SubstringLeetCodeAlphabet
- D. Non-Palindromic Substring
- LeetCode Longest Substring Without Repeating Characters(003)解法總結LeetCode
- [LeetCode] 3. Longest Substring Without Repeating Characters 題解LeetCode
- Longest Substring Without Repeating Characters
- 【LeetCode從零單排】No 3 Longest Substring Without Repeating CharactersLeetCode
- 【Leetcode】3. Longest Substring Without RepeatingCharacters無重最長子串LeetCodeGC
- #3 Longest Substring Without Repeating Characters[M]
- 3. Longest Substring Without Repeating Characters
- 149 Longest Substring Without Repeating Characters
- LintCode-Longest Common Substring
- [LeetCode] Longest Substring Without Repeating Characters 最長無重複字元的子串LeetCode字元
- LeetCode3:Longest Substring Without Repeating Characters(無重複字元的最長子串)LeetCode字元
- Leetcode Longest Common PrefixLeetCode
- [LeetCode]Longest Common PrefixLeetCode
- Leetcode 14 Longest Common PrefixLeetCode
- Leetcode 32 Longest Valid ParenthesesLeetCode
- LeetCode- Longest Absolute File PathLeetCode
- LeetCode-Longest Increasing SubsequenceLeetCode