LeetCode OJ : 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 exists one unique longest palindromic substring.
求字串中的最長迴文字串;
我的思路比較簡單,但是實現起來程式碼看著好亂。。。從第一個字元開始遍歷,直到最後一個字元,對每一個在index位置的字元進行判斷;
如果最長迴文字串是奇數,則判斷--index和++index是否相等;
如果最長迴文字串是偶數,則判斷index和++index是否相等;
class Solution {
public:
string longestPalindrome(string s) {
int maxlen = 0, index = 0, length;
if (s.size() < 3){
return s;
}
for (int i = 0; i < s.size()-1; ++i){
length = findPalindrome(s, i);
if (maxlen < length){
maxlen = length;
index = i;
}
}
if (maxlen % 2 == 0)
index = index - maxlen/2 + 1;
else
index = index - maxlen/2;
string longestSubString(s, index, maxlen);
return longestSubString;
}
int findPalindrome(string &s, int index){
int l_index = index-1, r_index = index+1;
int length1 = 1, length2 = 0;
/*針對abvba這種奇數的情況*/
while((s[l_index--] == s[r_index++]) && (r_index <= s.size())){
length1 += 2;
}
/*針對abba這種偶數的情況*/
l_index = index;
r_index = index+1;
while((s[l_index--] == s[r_index++]) && (r_index <= s.size())){
length2 += 2;
}
return (length1 > length2) ? length1 : length2;
}
};
雖然程式碼看著惡習,但是執行效率還闊以,Runtime: 72 ms;我再找找其它的方法;
相關文章
- LeetCode 5 (Longest Palindromic Substring)LeetCode
- Leetcode5: Longest Palindromic Substring(最長迴文子串)LeetCode
- [LeetCode] 5. Longest Palindromic SLeetCode
- Leetcode 3 Longest Substring Without Repeating CharactersLeetCode
- [LeetCode] 2414. Length of the Longest Alphabetical Continuous SubstringLeetCodeAlphabet
- Leetcode 3. Longest Substring Without Repeating CharactersLeetCode
- Leetcode javascript 3 longest-substring-without-repeating-charactersLeetCodeJavaScript
- LeetCode Longest Substring Without Repeating Characters(003)解法總結LeetCode
- [LeetCode] 3. Longest Substring Without Repeating Characters 題解LeetCode
- D. Non-Palindromic Substring
- 【Leetcode】3. Longest Substring Without RepeatingCharacters無重最長子串LeetCodeGC
- #3 Longest Substring Without Repeating Characters[M]
- LeetCode3:Longest Substring Without Repeating Characters(無重複字元的最長子串)LeetCode字元
- SPOJ 1811 Longest Common Substring(字尾自動機)
- Leetcode 32 Longest Valid ParenthesesLeetCode
- Leetcode 14 Longest Common PrefixLeetCode
- [LeetCode] 32. Longest Valid ParenthesesLeetCode
- Leetcode 30 Substring with Concatenation of All WordsLeetCode
- [LeetCode] 2419. Longest Subarray With Maximum Bitwise ANDLeetCode
- [LeetCode] 2831. Find the Longest Equal SubarrayLeetCode
- [LeetCode] 674. Longest Continuous Increasing SubsequenceLeetCode
- leetcode388. Longest Absolute File PathLeetCode
- Leetcode 298 Binary Tree Longest Consecutive SequenceLeetCode
- Leetcode 329. Longest Increasing Path in a MatrixLeetCode
- [LeetCode] 3239. Minimum Number of Flips to Make Binary Grid Palindromic ILeetCode
- LeetCode Longest Common Prefix(014)解法總結LeetCode
- [LeetCode] 524. Longest Word in Dictionary through DeletingLeetCode
- [LeetCode] 3090. Maximum Length Substring With Two OccurrencesLeetCode
- leetcode學習筆記14 Longest Common PrefixLeetCode筆記
- C# 寫 LeetCode easy #14 Longest Common PrefixC#LeetCode
- [leetcode] 1624. Largest Substring Between Two Equal CharactersLeetCode
- LeetCode - 014 - 最長公共字首(longest-common-prefix)LeetCode
- Leetcode 329. Longest Increasing Path in a Matrix (python+cpp)LeetCodePython
- LeetCode | 14. Longest Common Prefix的三種演算法LeetCode演算法
- OJ5-2 B. Shell sort
- Leet Code 3. Longest Substring Without Repeating Characters (最長的沒有重複字元的子字串)字元字串
- Js的substring和C#的SubstringJSC#
- LeetCode(1297):子串的最大出現次數 Maximum Number of Occurrences of a Substring(Java)LeetCodeJava
- JavaScript substring()JavaScript