Leetcode-Longest Palindromic Substring
2015-01-07 06:43
344 查看
public class Solution {
public String longestPalindrome(String s) {
int d=0,i=0,j;
boolean[][] array=new boolean[s.length()][s.length()];
String sub="";
for(d=0; d<s.length(); d++){
for(i=0; i<s.length()-d; i++){
j=i+d;
if(d==0 || (d==1&&s.charAt(i)==s.charAt(j))){
array[i][j]=true;
if(d + 1>sub.length())
sub = s.substring(i, i+d+1);
}else if(d>1&&array[i+1][j-1]&&s.charAt(i)==s.charAt(j)){
array[i][j]=true;
if(d +1>sub.length())
sub = s.substring(i, i+d+1);
}else
array[i][j]=false;
}
}
return d==1?s:sub;
}
}
-------------------------
算法,二维数组
==============
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 String longestPalindrome(String s) {
int d=0,i=0,j;
boolean[][] array=new boolean[s.length()][s.length()];
String sub="";
for(d=0; d<s.length(); d++){
for(i=0; i<s.length()-d; i++){
j=i+d;
if(d==0 || (d==1&&s.charAt(i)==s.charAt(j))){
array[i][j]=true;
if(d + 1>sub.length())
sub = s.substring(i, i+d+1);
}else if(d>1&&array[i+1][j-1]&&s.charAt(i)==s.charAt(j)){
array[i][j]=true;
if(d +1>sub.length())
sub = s.substring(i, i+d+1);
}else
array[i][j]=false;
}
}
return d==1?s:sub;
}
}
-------------------------
算法,二维数组
==============
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
- LeetCode 005 Longest Palindromic Substring
- LeetCode-Algorithms #005 Longest Palindromic Substring, Database #180 Consecutive Numbers
- leetcode 5 Longest Palindromic Substring
- leetcode 5-Longest Palindromic Substring
- LeetCode OJ 5 Longest Palindromic Substring
- Longest Palindromic Substring-----LeetCode
- LeetCode - Longest Palindromic Substring 题解
- leetCode:Longest Palindromic Substring
- [leetcode][string] Longest Palindromic Substring
- Leetcode: Longest Palindromic Substring
- [LeetCode]Longest Palindromic Substring
- LeetCode:Longest Palindromic Substring
- Leetcode5: Longest Palindromic Substring
- [LeetCode]Longest Palindromic Substring
- LeetCode-005 Longest Palindromic Substring
- leetcode——Longest Palindromic Substring
- LeetCode题解(Golang实现)--Longest Palindromic Substring
- LeetCode-Longest Palindromic subString(java)
- leetcode-5 longest palindromic substring