[leedcode 5]Longest Palindromic Substring

public class Solution {
    public String longestPalindrome(String s) {
        //本题是动态规划思想,构造一个数组pal[i][j],表示从i到j是否为一个回文,
        //pal[i][j]=true;if i=j;
        //pal[i][j]=true 当  s[i]==s[j]并且j-i=1;
        //pal[i][j]=true 当  j>i+1,s[i]==s[j]并且pal[i+1][j-1]=true;
        int start=0;
        int end=0;
        int maxLen=0;
        int len=s.length();
       boolean [][] pal=new boolean[len][len];
       for(int i=0;i<len;i++){
           for(int j=0;j<len;j++){
               pal[i][j]=false;
           }
       }
       for(int j=0;j<len;j++){
           //pal[j][j]=true;
         for(int i=0;i<=j;i++){

             /*if(i==j-1)pal[i][j]=s.charAt(i)==s.charAt(j);
             if(i<j-1){
                if(s.charAt(i)==s.charAt(j)&&pal[i+1][j-1])
                   pal[i][j]=true;
             }*/
             pal[i][j]=(s.charAt(i)==s.charAt(j)&&((j-i<2)||pal[i+1][j-1]));//次判断特别简介,利用了&&和||短路规则

            if(pal[i][j]&&maxLen<j-i+1){
                 maxLen=j-i+1;
                 start=i;
                 end=j;
                }

            }
         }

      return s.substring(start,end+1);
    }
}
时间: 2024-10-14 10:32:07

[leedcode 5]Longest Palindromic Substring的相关文章

【leedcode】 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. https://leetcode.com/problems/longest-palindromic-substring/ 求最大回文的长度,其实这道题

[string]Longest Palindromic Substring

Total Accepted: 82026 Total Submissions: 379898 Difficulty: 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. Subscrib

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

5. Longest Palindromic Substring - Unsolved

https://leetcode.com/problems/longest-palindromic-substring/#/description 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: &

[Leetcode] 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. 做这道题之前要先了解什么是回文子串.回文串通俗的解释是,分别从字符串两端开始遍历,得到的结果相同,如"abba",从两端的遍历结果都是:&q

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. 思路:(beat 2.63%) 以一个位置为准,考察两侧相同距离是否相同.这样会导致"aaaaaaaaaaaaaaaaaaaaaaaaaa...&qu

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

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