第一种:

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. iOS开发笔记:编译时出现的错误和解决办法

    1."std::ios_base::Init::~Init()", referenced from 出现这样的编译问题,是需要再加进libstdc++.dylib和libstdc+ ...

  2. util 学习

    const I = 3.4893589; console.log(Number.parseInt(I)); console.log(Number.parseFloat(I)); console.log ...

  3. Django Url编码问题

    Django Url编码问题   最近在学习Django,写一个blog程序练练手手.对于一个才开始接触web开发的来说,难免会遇到一些问题.   有一个这样的模板: {%for k,v in cat ...

  4. day8_python学习笔记_chapter11_函数

    1. 返回对象的数目   python实际返回的对象 0 -> None ; 1 -> object ; >1 -> tuple 2. 内部/内嵌函数:如果内部函数的定义包含了 ...

  5. [LeetCode]题解(python):149-Max Points on a Line

    题目来源: https://leetcode.com/problems/max-points-on-a-line/ 题意分析: 在一个2D的板上面有很多个点,判断最多有多少个点在同一条直线上. 题目思 ...

  6. Vmware 克隆CentOS 网络IP配置

    在VMware里克隆出来的CentOS Linux.. ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: service network restart Shuttin ...

  7. poj2328---"right on"进入下一个case的模板(while)

    #include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ]; ,end=; w ...

  8. iso-开发基础知识-1-程序流程

    main-应用程序委托-视图控制器 main()---主函数 应用程序委托  ---AppDelegate     视图控制器 ---ViewController - (BOOL)applicatio ...

  9. MySQL 5.7.10 免安装配置

    # 配置环境:windows 64bit # 安装版本:mysql-5.7.10-win32(zip archive版本) 1. ZIP Archive版是免安装的,只需把mysql-5.7.10-w ...

  10. UVa 10330 - Power Transmission(最大流--拆点)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...