BAT面试算法进阶(5)- 最长回文子串(方法一)

一.算法题

  • 题目

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.面试

  • Example

Example1:算法

Input: "babad"编程

Output: "bab"bash

Note: "aba" is also a valid answer.学习

Example2:ui

Input: "cbbd"spa

Output: "bb"3d

二.算法题解读

  • 题目大意:给定一个字符串S,找出S串中最长的回文子串.你能够假设s的最大长度为1000.

Example1: 输入: "babad"code

输出: "bab"orm

注意: "aba" 是一个有效答案.

Example2:

输入: "cbbd"

输出: "bb"

三.回文字符串

Enter your image description here:

四.找到字符串的最长公共子串

通常开发者,能想到的最快速的方法,就是找到"最长公共子串".

"反转S并成为S',找到S和S'之间的最长公共子串.它也必须是最长的回文子串"

Enter your image description here:

注意: 若是咱们并非全部的最长公共子串,就必定是最长回文子串.

Enter your image description here:

因此,若是只是单纯的查找最长公共子串方法,是不可行的.可是,若是去修改这个问题?

思路: 在咱们找到一个最长的公共子串候选者时,咱们检查子串的索引是否与反向子串的原始索引相同.若是是,那么尝试更新到目前为止发现的最长的回文.若是没有,咱们就跳过这个,寻找下个候选回文子串.

五.动态编程解决方案

  • 若是子串 Si...Sj 是回文,则定义P[i,j]为真,不然为假
  • 因此,P[i,j] <-- (p[i+1,j-1] 和 Si = Sj);

六.复杂度

时间复杂度:O(N*N)

空间复杂度:O(N*N)

七.代码

C Code

string longestPalindromeDP(string s) {
  int n = s.length();
  int longestBegin = 0;
  int maxLen = 1;
  bool table[1000][1000] = {false};
  for (int i = 0; i < n; i++) {
    table[i][i] = true;
  }
  for (int i = 0; i < n-1; i++) {
    if (s[i] == s[i+1]) {
      table[i][i+1] = true;
      longestBegin = i;
      maxLen = 2;
    }
  }
  for (int len = 3; len <= n; len++) {
    for (int i = 0; i < n-len+1; i++) {
      int j = i+len-1;
      if (s[i] == s[j] && table[i+1][j-1]) {
        table[i][j] = true;
        longestBegin = i;
        maxLen = len;
      }
    }
  }
  return s.substr(longestBegin, maxLen);
}

复制代码

八.学习建议

尝试画图->阅读代码

  • 算法并非1+1=2.不少时候须要你们拿好纸笔思考才能感觉到它的魅力之处!
  • 这次附上小编学习的时候草稿! 你们也一块儿吧....

Enter your image description here:
感谢你们观看这一篇文章,给你们献上了iOS开发的188道面试题哦! 加小编的群就能够直接获取哦!551346706
Enter your image description here:
Enter your image description here:
相关文章
相关标签/搜索