LeetCode(3)题解: Longest Palindromic Substring

https://leetcode.com/problems/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.

思路:

我的思路是遍历字符串,对每个元素用扩张法,找到以这个位置作为回文序列中心左边第一个点的回文序列,因为牵扯到奇偶性,需要遍历两遍。复杂度是O(n^2),但是一般情况这样剪枝比较快。

AC代码:  60ms C++

 1 class Solution {
 2 public:
 3     string longestPalindrome(string s) {
 4         int loc,j,l=0,n=s.size();
 5         if (n==0)
 6             return "";
 7         if (n==1)
 8             return s;
 9         for (int i=0;i<n;i++){
10             for(j=0; i-j>=0 && i+j+1<n;j++){
11                 if(s[i-j]!=s[i+j+1])
12                     break;
13             }
14             if(2*j>l){
15                 l=2*j;
16                 loc=i;
17             }
18         }
19         for (int i=0;i<n;i++){
20
21             for(j=0; i-j>=0 && i+j+2<n;j++){
22                 if(s[i-j]!=s[i+j+2])
23                     break;
24             }
25             if(2*j+1>l){
26                 l=2*j+1;
27                 loc=i;
28             }
29         }
30         return s.substr(loc-l/2+1,l);
31     }
32 };
时间: 2024-10-06 06:20:09

LeetCode(3)题解: Longest Palindromic Substring的相关文章

【LeetCode OJ】Longest Palindromic Substring

题目链接:https://leetcode.com/problems/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. 解题思路:p

【LeetCode】005 Longest Palindromic Substring

题目: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, and there exists one unique longest palindromic substring. 题意:字符串S,找到S中的最长回文子串.假定S最长为100

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

【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. 思路:用Manacher算法得到最大回文子串的长度,得到带#的最大回文子串,用split剔除#后转化成String形式输出即可. public

【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. 解题分析: 之前百度实习生面试也被问到这个问题.这个题比较简单.就是在分别考虑对称字符字串为奇数和偶数的情况,在不超过字符串范围的情况下向

LeetCode No.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. 最长回文子串. 有两种思路: 1,从左向右找对称点,从对称点(或者两个相同的相邻字符)向两边搜索,记下搜到的最大长度. 2,设置一个二维bool数组,

LeetCode 题解之 5. Longest Palindromic Substring

5. Longest Palindromic Substring 题目描述和难度 题目描述: 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设?s 的最大长度为1000. 示例 1: 输入: "babad"输出: "bab"注意: "aba"也是一个有效答案. 示例 2: 输入: "cbbd"输出: "bb" 题目难度:中等. 英文网址:5. Longest Palindromic Substri

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. 在给定一个字符串S中,找到最长的回文串. 很恶心的一道题,不过应该都能想到枚举,从第一个开始枚举.枚举的字母向两头延伸,如果相同则继续,不同则开始下一

Longest Palindromic Substring leetcode java

题目: 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. 题解: 第一种方法就是挨个检查,维护全局最长,时间复杂度为O(n3),会TLE. 代码如下: 1 public String longestP