LeetCode OJ: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.

玩了两天dota2,罪过罪过,还是应该老老实实刷题啊。

题目求得是最长的回文子串,这里使用的是暴力的解法,也就是解决两种回文"asdsa"以及"assa",即奇数以及偶数回文,相当于用了二重循环,代码如下:

 1 class Solution {
 2 public:
 3     string longestPalindrome(string s) {
 4         if (!s.size()) return "";
 5         string ret;
 6         string tmp;
 7         for (int i = 0; i < s.size(); ++i){
 8             tmp = findPal(s, i - 1, i + 1);
 9             if (tmp.size() > ret.size())
10                 ret = tmp;
11             tmp = findPal(s, i , i + 1);
12             if (tmp.size() > ret.size())
13                 ret = tmp;
14         }
15         return ret;
16     }
17
18     string findPal(string & s,  int left, int right)
19     {
20         if (left < 0)
21             return s.substr(left + 1, 1);
22         if (right >= s.size())
23             return s.substr(right - 1, 1);
24
25         while (left >= 0 && right < s.size()){
26             if (s[left] != s[right])
27                 break;
28             left--;
29             right++;
30         }
31         left++;
32         return s.substr(left, right - left);
33     }
34 };
时间: 2024-10-22 16:08:37

LeetCode OJ:Longest Palindromic Substring(最长的回文字串)的相关文章

[C++]LeetCode: 99 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. 思路:题目要求的s的一个最长回文子串.暴力解决办法就是枚举所有的子串,再对每个子串进行回文判断.进行剪枝,我们考虑可以使用动态规划来避免重复的判

[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 exists one unique longest palindromic substring. 最长回文子串Longest palindromic substring, 最长回文子串或最长对称因子问题是在一个字符串中查找一个最长连续子串,这个子串

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 exists one unique longest palindromic substring. 思路一:(超时)简单的截取长度为length(length递减)的字串,判断其是否为回文串.第一个满足要求的回文串最长. public class S

[LeetCode]33. 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. 解法一:考虑回文字符串paliStr的特征,分为字符串长度为奇偶两种情况:(1)paliStr.size()为奇数时,则从最中间的一个字符往两边扩展是

Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

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: "babad" Output: "bab" Note: "aba" is also a valid answer. Example 2: Input: "cbbd"

Leetcode 5 Longest Palindromic Substring (最长回文子字符串)(动态规划)

Leetcode 5 题目描述 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: "babad" Output: "bab" Note: "aba" is also a valid answer. Example 2: Inp

LeetCode:5_Longest Palindromic Substring | 最长的回文子串 | Medium

题目: 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. 解题思路:1.简单思路:暴力破解法,时间复杂度O(n^3),肯定通不过. 2.动态规划法:(一般含“最XX”等优化词义的题意味着都可以动态规划

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" Ou

LeetCode 5 Longest Palindromic Substring 最长回文子序列 manacher算法 string.substr 难度:2

https://leetcode.com/problems/longest-palindromic-substring/ manacher算法:http://blog.csdn.net/ywhorizen/article/details/6629268 string longestPalindrome(string s) { char ch[2001];int p[2001]; ch[2*s.size()] = 0; for(int i = 0; i < 2 * s.size(); i++) {

LeetCode #5 Longest Palindromic Substring (M)

[Problem] 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. [Analysis] 这题的思路有很多种,网上也有各种讨论.这里我采用的是个人觉得比较好理解的一种利用Dynamic Progra