您的位置:首页 > 其它

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: