LeetCode 005 Longest Palindromic Substring - Java

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"

定位:中等题

找到给定的字符串中的最长的回文子串,可以采取遍历每一个字符,判断有没有可能以其为中心生成回文字串,一旦可能就向两端扩展。有两种情况,一种是以一个字母为中心,另一种为以两个相同字母为中心,当扩展到极限后记录并判断是不是最长的。

Java实现如下:

 1 public class Solution {
 2     public String longestPalindrome(String s) {
 3         int len=s.length();
 4         if(len<=1){
 5             return s;
 6         }
 7         int Len=1,maxLen=1;
 8         int i,j;
 9         String str=String.valueOf(s.charAt(0));
10         if(s.charAt(0)==s.charAt(1)){
11             str=s.substring(0,2);
12             maxLen=2;
13         }
14         for(i=1;i<len-1;i++){
15             if(s.charAt(i-1)==s.charAt(i+1)){
16                 j=1;
17                 while(i-j>=0&&i+j<len&&s.charAt(i-j)==s.charAt(i+j)){
18                     j++;
19                 }
20                 j--;
21                 Len=2*j+1;
22                 if(Len>maxLen){
23                     maxLen=Len;
24                     str=s.substring(i-j,i+j+1);
25                 }
26             }
27             if(s.charAt(i)==s.charAt(i+1)){
28                 j=1;
29                 while(i-j+1>=0&&i+j<len&&s.charAt(i-j+1)==s.charAt(i+j)){
30                     j++;
31                 }
32                 j--;
33                 Len=2*j;
34                 if(Len>maxLen){
35                     maxLen=Len;
36                     str=s.substring(i-j+1,i+j+1);
37                 }
38             }
39         }
40         return str;
41     }
42 }
时间: 2024-10-14 04:41:45

LeetCode 005 Longest Palindromic Substring - Java的相关文章

Java for 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. 解题思路一: 暴力枚举 共N^2量级个子串(从下标零开始),每次检查需一个for循环,等于是3重for循环,时间复杂度O(n^3) 解题思路二: 动态

[LeetCode] 005. Longest Palindromic Substring (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 005.Longest_Palindromic_Substring (Medium) 链接: 题目:https://oj.leetcode.com/problems/Longest-Palindromic-Substring/ 代码(github):https://github.com/illuz/leetcode

leetcode 5. Longest Palindromic Substring [java]

public String longestPalindrome(String s) { String rs = ""; int res = 0; for(int i = 0; i< s.length(); i++){ int left = i, right = i; while(left >= 0 && s.charAt(i) == s.charAt(left)) left --; while(right < s.length() &&

【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【5】. Longest Palindromic Substring --java实现

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. 题目要求给定字符串的最大对称子字符串,如"aaabccbac

Leetcode: Longest Palindromic Substring. 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. 动态规划public class Solution { public String longestPalindrome(String s) { if

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

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之余全部耗在这上面了,只怪自己基础差.本文主要介绍使用Manacher线性算法来求解字符串的最长回文子字符串. 题目 Given a string S, find the longest palindromic substring in S. You may assume that the maxim

LeetCode:Longest Palindromic Substring

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. 动态规划解法 T(n) = O(n^2)  ,S(n) = O(n^2); Solutio