首页 » 编程之美 » 正文

[leetcode_5]Longest Palindromic Substring

哇,这个题牛逼了,也做了一个下午。
其实就是给一个长串。然后找出最长的回文字串。

最开始想到的办法是暴力,反正做题嘛,先做出来再说。
怎么暴力呢,最开始的想法是:
一个固定的函数check判断该串是不是回文。
s如果是,那么就是了。
不然check(s(0,s.length()-2))和s(1,s.length()-1)
这个代码写出来,然后试了试,很短的串没问题,但是仔细想想这个复杂度太高了,特别是串稍微长点点。
果然前期超时。
超时的话,就是方法不对,需要改思路。
那我们就逆思维:
先判断
s[i]是不是回文,然后两边各+1
s[i] s[i+1]是不是回文,然后两边各+1
之所以有两种情况是因为奇和偶的问题。
欧克,这样大部分情况应该时间会优化一些。
但还是超时,这样其实离成功更进一步了。
下面再接着想,其实每次不一定是+1,如果我当前已经找到了长为sum的串,那么枚举下一个节点的时候,其实我可以从+sum/2来查询,果然AC啦~~~~~~~
WA的几次就是边界没写好~~~~

class Solution {
public:
    int sum;
    int step;
    string ans;
    string longestPalindrome(string s) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        sum = 0;
        for(int i = 0;i < s.length();i++)
        {
            step = sum/2;
            if(i-step < 0 || i+step >= s.length())
                continue;
            func(s,i-step,i+step);<br />
        }
        for(int i = 0;i &amp;lt; s.length();i++)
        {
            step = sum/2;
            if(i-step &amp;lt; 0 || i+step+1 &amp;gt;= s.length())
                continue;
            func(s,i-step,i+1+step);<br />
        }
        return ans;
    }<br />
    void func(string s,int x,int y)
    {
        if(x &amp;lt; 0 || y &amp;gt; s.length()-1)return;
        if(checkp(s.substr(x,y-x+1)))
        {
            if(sum &amp;lt; y-x+1)
            {
                sum = y-x+1;
                ans = s.substr(x,y-x+1);
            }
            func(s,x-1,y+1);
        }
    }
    bool checkp(string s)
    {
        int i = 0;
        int j = s.length() - 1;
        while(i &amp;lt;= j)
        {
            if(s[i] != s[j])
                return false;
            i++;
            j--;
        }
        return true;
    }
};

发表评论