Leetcode---Longest Palindromic Substring

寻找最长回文子串
本文探讨了寻找字符串中最长回文子串的有效算法。首先介绍了动态规划方法,并讨论了其时间复杂度问题。随后提出了一种朴素的方法,通过遍历每个字符作为中心来判断回文,适用于奇数和偶数长度的子串。
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.

首先我用了DP,没想到存储没报错,时间报错了,TLE了。

原理是记录所有回文,然后更新最大值。

比较值得注意的是
  1. for(int i=n-2;i>=0;i--){
  2.         for(int j=i+1;j<=n-1;j++){
  3.             dp[i][j]=dp[i+1][j-1]&&(s[i]==s[j]);
i依赖于i+1, j依赖于j-1,所以i--,j++的遍历

  1. string longestPalindrome(string s) {
  2.     int n=s.length();
  3.     vector<vector<bool>> dp(n,vector<bool>(n,false));
  4.     for(int i=0;i<n;i++){
  5.         for(int j=0;j<=i;j++){
  6.             dp[i][j]=true;
  7.         }
  8.     }
  9.     int num=1;
  10.     int x=0;
  11.     int y=0;
  12.     for(int i=n-2;i>=0;i--){
  13.         for(int j=i+1;j<=n-1;j++){
  14.             dp[i][j]=dp[i+1][j-1]&&(s[i]==s[j]);
  15.             if(dp[i][j] && (j-i+1>num)){
  16.                 num=j-i+1;
  17.                 x=i;
  18.                 y=j;
  19.             }
  20.         }
  21.     }
  22.     return s.substr(x,num);
  23. }
后来发现其实朴素的判断回文空间复杂性更小。其实就是从0到n-1遍历,每个节点为中心判断回文,分为奇偶两种情况,

奇数遍历:


  1. for (int i = 1; i < s.length(); ++i){
  2.         low = i - 1;
  3.         high = i + 1;
  4.         while (low >= 0 && high <s.length() && s[low] == s[high])
  5.         {
  6.             //是回文
  7.             --low;
  8.             ++high;
  9.         }
  10. }



数遍历:


  1. for (int i = 1; i < s.length(); ++i){
  2.         low = i - 1;
  3.         high = i;
  4.         while (low >= 0 && high <s.length() && s[low] == s[high])
  5.         {
  6.             //是回文
  7.             --low;
  8.             ++high;
  9.         }
  10. }



代码如下:

  1. string longestPalindrome(string s) {

  2. int start = 0;
  3.  int maxLength = 1;
  4. string res;

  5.     if(s.length()==1)
  6.        return s;
  7.     int low, high;
  8.     for (int i = 1; i < s.length(); ++i)
  9.     {
  10.         low = i - 1;
  11.         high = i;
  12.         while (low >= 0 && high < s.length() && s[low] == s[high])
  13.         {
  14.             if (high - low + 1 > maxLength)
  15.             {
  16.                 start = low;
  17.                 maxLength = high - low + 1;
  18.             }
  19.             --low;
  20.             ++high;
  21.         }
  22.          low = i - 1;
  23.         high = i + 1;
  24.         while (low >= 0 && high <s.length() && s[low] == s[high])
  25.         {
  26.             if (high - low + 1 > maxLength)
  27.             {
  28.                 start = low;
  29.                 maxLength = high - low + 1;
  30.             }
  31.             --low;
  32.             ++high;
  33.         }
  34.     }
  35.      res = s.substr(start,maxLength);
  36. return res;


  37. }

<script>window._bd_share_config={"common":{"bdsnskey":{},"bdtext":"","bdmini":"2","bdminilist":false,"bdpic":"","bdstyle":"0","bdsize":"16"},"share":{}};with(document)0[(getelementsbytagname('head')[0]||body).appendchild(createelement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new date()/36e5)];</script>
阅读(4) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值