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 (s == null || s.length() == 0) return "";
int n = s.length();
int max = 0, start = 0, end = 0;
boolean[][] c = new boolean[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
c[i][j] = i >= j ? true : false;
}
}
//c[i][j] 记录从第i个到第j个是不是回文。
for (int j = 1; j < n; j++) {
for (int i = 0; i < j; i++) {
if (s.charAt(i) == s.charAt(j) && c[i+1][j-1]) {
c[i][j] = true;
if (j - i + 1 > max) {
max = j - i + 1;
start = i;
end = j;
}
}
else
c[i][j] = false;
}
}
return s.substring(start, end+1);
}
}

Leetcode: Longest Palindromic Substring. java

时间: 2024-10-11 16:21:29

Leetcode: Longest Palindromic Substring. java的相关文章

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 &quot;Longest Palindromic Substring&quot; - 1D DP

2D DP is an intuitive solution of course, but I got an MLE error, so I simplified it into a 1D DP: class Solution { public: void goDp(vector<int> &dp, int &maxLen, int &is, int &ie, int startLen, int len, string &s) { for(int ile

[LeetCode] Longest Palindromic Substring [14]

题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length ofS is 1000, and there exists one unique longest palindromic substring. 原题链接 解题思路 最长回文字串,相信做过Palindrome Partitioning II 这个题的同学应该可以很快做出来.没错,这个题还可以

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]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. 基本思想: 本题的navie解法是对每个(i,j) i<=j 测试是否是回文串,并记录最大的情况.O(n^3) 如果从回文串的中心考虑往两

Leetcode: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. 翻译:求给定

[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

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. 给定一个字符串S,找出当中的最长回文字符子串. 1.枚举全部子串,并推断是否是回文串同一时候记录最大长度.超时. //找出全部子串,并推断是否是回文串

[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. http://fisherlei.blogspot.com/2012/12/leetcode-longest-palindromic-substrin