leetcode05- Longest Palindromic Substring之Java版本

我的leetcode之旅,该篇章主要完成使用Java实现算法。这是第5篇 Longest Palindromic Substring

全部代码下载:Github链接:github链接,点击惊喜;写文章不易,欢迎大家采我的文章,以及给出有用的评论,当然大家也可以关注一下我的github;多谢;

1.题目简介:

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.

2.我的思路:

1.最长回文字符串解法

2.一看到最长回文字符串我就想到了Manacher算法

3.算法思想:请见http://blog.sina.com.cn/s/blog_3fe961ae0101iwc2.html

3.我的AC代码

package com.rlovep.string;
/**
 * Longest Palindromic Substring
 * 我的思路:
 * 1.最长回文字符串解法
 * 一看到最长回文字符串我就想到了Manacher算法
 * 算法思想:请见我转载的博文
 * @author peace
 *
 */
public class PalindromicSubstring {
     public String longestPalindrome(String input) {
            int pos=0;//最右回文子串的中心位置
                int maxRight=0;//最右回文子串的最右端位置
                int maxLength=0;//字符串最大的回文长度
                int finpos=0;
                int finMaxRight=0;
                //添加分隔符
                StringBuilder sb=new StringBuilder();
                sb.append("#");
                for(int i=0;i<input.length();i++){
                    sb.append(input.charAt(i));
                    sb.append("#");
                }
                //获得字符串数组
                input=sb.toString();
                char[] s= input.toCharArray();
                int rl[] =new int[s.length];//存储每个位置的最大回文子串半径
                for(int i=0;i<s.length;i++){
                    if(i<maxRight){
                        rl[i]=Math.min(rl[2*pos-i], maxRight-i);//核心代码
                    }
                    else{
                        rl[i]=1;
                    }
                    while((i-rl[i])>=0&&//不能超过左端
                          (i+rl[i])<s.length&&//不能超过右端
                          (s[i-rl[i]]==s[i+rl[i]]))
                    {
                        rl[i]+=1;
                    }
                    //更新最右回文子串的最右端位置
                    if((i+rl[i]-1)>maxRight){
                        pos=i;
                        maxRight=i+rl[i]-1;
                    }
                    if(rl[i]>maxLength){
                        finpos=i;
                        finMaxRight=maxRight;
                        maxLength=rl[i];
                    }
                }
            input=input.substring(finpos+1-maxLength,finMaxRight);
            return input.replace("#", "");
        }
}

好的本章介绍到这里 来自伊豚wpeace(blog.wpeace.cn)

时间: 2024-08-29 15:42:46

leetcode05- Longest Palindromic Substring之Java版本的相关文章

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

leetcode longest palindromic substring (medium) /java

最长回文字串 上题: 测试用例中,注意aaabaaaa. 但是我time limit exceeded.用了极暴力的方法解.(三层循环)找每一个字符的最长回文字串. 1 /** 2 * 最长回文子串 3 * 2017-5-7 4 **/ 5 6 import java.io.*; 7 import java.util.*; 8 import java.lang.*; 9 10 public class Solution 11 { 12 public static String longestPa

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

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 5 Longest Palindromic Substring(C,C++,Python,Java)

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. Solution: 以每一个字符为中间查找回文串,然后记录下最大的回文串,时间复杂度O(n^2) 题目大意: 给定一个字符串,求字符

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: 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

5. Longest Palindromic Substring java solutions

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 public class Solution { 2 public String longestPalindrome(String s) { 3 i

[Java]LeetCode5 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. 题意:求字符串中最长的回文 感觉这题不难,可以这样想,设置两个指针,分别对应0,len-1. 比如从第一个字符开始,abababac,我们可以找a出现