第一种:

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. c++中类模版中的static数据成员的定义

    这个有点绕. 如下: template <typename T> class A{ ......... static std::allocate<T> alloc_; }; t ...

  2. Silverlight代码编写对控件的PlaneProjection.RotationY属性控制动画

    Canvas c; void btnDraw_Click(object sender, RoutedEventArgs e) { Storyboard story = new Storyboard() ...

  3. Couldn't read row 0, col -1 from CursorWindow

    java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Curso ...

  4. atoi函数和atof函数

    1.函数名:atoi 功能:是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中 名字来源:alphanumeric to integer 用法:int atoi(const char *n ...

  5. Orchard 源码探索(Log)

    简单工厂模式.抽象工厂模式和适配器模式 依赖倒置原则也叫依赖倒转原则,Dependence Inversion Principle,对抽象进行编程,不要对实现进行编程. A.高层次的模块不应该依赖于低 ...

  6. 美国地质调研局USGS

    https://lta.cr.usgs.gov/get_data/ http://www.usgs.gov/

  7. 继承CWnd自绘按钮

    头文件: //头文件 #pragma once // CLhsButton #define MYWM_BTN_CLICK WM_USER+3001 //关闭按钮单击响应 //tab按钮的状态 enum ...

  8. 运用JavaScript构建你的第一个Metro式应用程序(onWindows 8)(三)

    原文 http://blog.csdn.net/zhangxin09/article/details/6793593 这是<运用 JavaScript构建你的第一个Metro式应用程序>系 ...

  9. MYSQL 巧用count,sum进行统计数据

    SELECT a.user,count(b.order_id) as subcount,sum(if(b.verifysta='Y',1,0)) as passcount FROM vicidial_ ...

  10. Java用Dijkstra算法实现地图两点的最短路径查询(Android版)

    地图上实现最短路径的查询,据我了解的,一般用Dijkstra算法和A*算法来实现.由于这是一个课程项目,时间比较急,而且自己不熟悉A*算法,所以参考网上的Dijkstra算法(http://blog. ...