第一种:

public static void main(String[] args) {
String s = "abcbaaaaabcdcba";
int n,m;
String re = "";
for(int i = ; i < s.length();i++){
for(int j = i+;j< s.length();j++){
n = i;
m = j;
for(;j > i;j--,i++){
if(s.charAt(i) != s.charAt(j))
break;
}
if(j <= i){
if(m-n > re.length())
re = s.substring(n, m+);
}
}
}
System.out.println(re);
}

看的是国外的一篇博客,他把这种方法叫做是Naive Approach,这是最容易想到的一个方法,效率确实不怎样,时间复杂度是O(n^3)级。

第二种方法:

public static void main(String[] args) {

        String s = "abcbaaaaabcdcba";

        int[][] table = new int[s.length()][s.length()];
int i, j;
for (i = ; i < s.length(); i++)
for (j = ; j < s.length(); j++)
table[i][j] = ;
for (i = ; i < s.length(); i++) {
for (j = ; j < s.length(); j++) {
if (j + i >= s.length()) {
break;
}
if (j == j + i)
table[j][j + i] = ;
else if (j + == j + i) {
if (s.charAt(j) == s.charAt(j + i))
table[j][j + i] = ;
} else {
if (s.charAt(j) == s.charAt(j + i)
&& table[j + ][j + i - ] == )
table[j][j + i] = ;
}
}
}
for (i = ; i < s.length(); i++) {
for (j = ; j < s.length(); j++) {
System.out.print(table[i][j] + " ");
table[i][j] = ;
}
System.out.println();
}
}

这个算法的思路:

构建一个n*n的表格,表格中table[i][j] == 1代表子串(i,j)是回文串,table[i][j] ==0即代表子串(i,j)不为回文串,并且有这样的推算规则:

1)table[i][i]必为1;

2)table[i][i+1]==1的满足条件是:s.charAt(i) == s.charAt(i+1);

3)其他table[i][j] == 1 的满足条件是:s.charAt(i) == s.charAt(j) && table[i+1][j-1] == 1.

该算法的时间复杂度为 O(n^2), 空间复杂度 O(n^2)。

面试常用算法——Longest Palindromic Substring(最长回文子串)的更多相关文章

  1. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  2. 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  3. LeetCode:Longest Palindromic Substring 最长回文子串

    题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  4. lintcode :Longest Palindromic Substring 最长回文子串

    题目 最长回文子串 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串. 样例 给出字符串 "abcdzdcab",它的最长回文 ...

  5. [LeetCode] 5. Longest Palindromic Substring 最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  6. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  7. [leetcode]5. Longest Palindromic Substring最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  8. 【翻译】Longest Palindromic Substring 最长回文子串

    原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...

  9. 1. Longest Palindromic Substring ( 最长回文子串 )

    要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...

  10. 005 Longest Palindromic Substring 最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

随机推荐

  1. SQL 语句整理

    1.       /*  SQL CASE 语句写法 * SELECT TABLE1.USER_ID, TABLE1.COMP_CODE, TABLE1.DEPT_CODE, TABLE1.USER_ ...

  2. fragment详解(官方文档)

    原作者为: 苍山.感谢他分享的内容,现在分享出来给eoeAndroid的各位同胞. 详情参考http://www.eoeandroid.com/thread-71642-1-1.html和http:/ ...

  3. OC语法1——OC概述

    Object-C简介: OC,即Object-C,iOS开发的核心语言.它是基于C语言的,在C的基础上做了面向对象的封装,所以OC是面向对象的语言.同时也因此OC是兼容C的,也就是说在iOS开发中,可 ...

  4. 动态Script标签 解决跨域问题

     动态Script 解决跨域问题 1.动态创建scriptcreateScript : function(src){ var varScript = document.createElement(&q ...

  5. git彻底删除commit记录的方法

    在github上,如果非默认分支的话,直接用以下方法: git reset --hard <commit_id> git push origin HEAD --force 如是默认分支,需 ...

  6. cad2007 钢筋符号显示为问号

    如题:cad2007 钢筋符号显示为问号 解决办法:下载Tssdeng,解压(Tssdeng.rar) 把下载到的cad大字体Tssdeng.shx文件放到autoCAD2007安装目录C:\Prog ...

  7. Centos 升级MySQL版本或者Yum安装Mysql5.6

    Centos 升级MySQL版本或者Yum安装Mysql5.6 1.从MySQL Yum仓库下载最新的rpm文件:http://dev.mysql.com/downloads/repo/yum/Cen ...

  8. poj 3323 Matrix Power Series (矩阵乘法 非递归形式)

    为了搞自动机+矩阵的题目,特来学习矩阵快速幂..........非递归形式的求Sum(A+A^2+...+A^k)不是很懂,继续弄懂................不过代码简洁明了很多,亮神很给力 # ...

  9. CCNA实验(1) -- 基本配置

    Ctrl+A: 到行首(Ahead)Ctrl+E: 到行尾(End)Esc+B: 回退一个单词(Back)Esc+F: 前进一个单词(Forward) 1.三种配置模式2.时间时区配置3.设置超时时间 ...

  10. c语言libcurl 使用实例get/post方法+c语言字符串处理

    #include <stdio.h> #include <curl/curl.h> #include <string.h> #include <ctype.h ...