{"id":221,"date":"2021-08-03T11:14:32","date_gmt":"2021-08-03T03:14:32","guid":{"rendered":"http:\/\/blog.nonot.cn\/?p=221"},"modified":"2024-07-08T12:53:11","modified_gmt":"2024-07-08T04:53:11","slug":"php%e7%ae%97%e6%b3%95%e9%a2%98%ef%bc%9a%e6%9c%80%e9%95%bf%e5%9b%9e%e6%96%87%e5%ad%90%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2021\/08\/03\/php%e7%ae%97%e6%b3%95%e9%a2%98%ef%bc%9a%e6%9c%80%e9%95%bf%e5%9b%9e%e6%96%87%e5%ad%90%e4%b8%b2\/","title":{"rendered":"php\u7b97\u6cd5\u9898\uff1a\u6700\u957f\u56de\u6587\u5b50\u4e32"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"item-1\">\u6700\u957f\u56de\u6587\u5b50\u4e32<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32 s\uff0c\u627e\u5230 s \u4e2d\u6700\u957f\u7684\u56de\u6587\u5b50\u4e32\u3002\u4f60\u53ef\u4ee5\u5047\u8bbe s \u7684\u6700\u5927\u957f\u5ea6\u4e3a 1000\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b 1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u8f93\u5165: \"babad\"\n\u8f93\u51fa: \"bab\"\n\u6ce8\u610f: \"aba\" \u4e5f\u662f\u4e00\u4e2a\u6709\u6548\u7b54\u6848\u3002<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b 2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u8f93\u5165: \"cbbd\"\n\u8f93\u51fa: \"bb\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"item-1-1\">\u4e00\u3001\u6cd5\u4e00\u66b4\u529b\u7834\u89e3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u5c31\u4e0d\u7528\u591a\u89e3\u91ca\u4e86<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Solution {\n\n    \/**\n     * \u6cd5\u4e00\uff1a\n     * @param String $s\n     * @return String\n     *\/\n    function longestPalindrome($s) {\n        $len = strlen($s);\n        $tmps = '';\n        $max = 0;\n        for($i=0 ; $i&lt;$len; $i++){ \/\/\u8d77\u59cb\u4e0b\u6807\n            for($j=$i+1; $j&lt;=$len;$j++){ \/\/\u957f\u5ea6\n                if($this-&gt;isPalindrome(substr($s,$i,$j)) &amp;&amp; $j &gt; $max){\n                    $tmps = substr($s,$i,$j);\n                    $max = $j;\n                }\n            }\n        }\n        return $tmps;\n    }\n\n    function isPalindrome($subs){\n        $len = strlen($subs);\n        for($i=0; $i&lt;(int)($len\/2); $i++){\n            if($subs[$i] != $subs[$len-$i-1]){\n                return false;\n            }\n        }\n        return true;\n    }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"item-1-2\">\u4e8c\u3001\u6700\u957f\u516c\u5171\u5b50\u4e32<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u601d\u8def\uff1a\u5c31\u662f\u628a\u5b57\u7b26\u4e32\u53cd\u8f6c\u8fc7\u6765\uff0c\u4f8b\u5982 s=\u2018ababc\u2019\uff0c\u53cd\u6b63s\u2018=\u2019cbaba\u2018\uff0c\u4e24\u4e2a\u5b57\u7b26\u4e32\u5de6\u53f3\u91cd\u5408\uff0c\u5f97\u51fa\u6700\u957f\u7684\u5b50\u4e32\u5c31\u662f\u7b54\u6848\uff0c\u4e5f\u5c31\u662fbab\uff08\u6216aba\uff09\u3002\u4f46\u662f\u6709\u53ef\u80fd\u4f1a\u51fa\u73b0==\u7279\u4f8b\uff08\u5982\uff1aS = abc435cba\uff0cS&#8217; = abc534cba\uff09==\uff0c\u91cd\u5408\uff0c\u5b50\u4e32\u4e3aabc\u3002\u4e0d\u7b26\u5408\u6761\u4ef6\uff0c\u9700\u8981\u52a0\u5224\u65ad<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Solution2 {\n\n    \/**\n     * @param String $s\n     * @return String\n     *\/\n    function longestPalindrome($s) {\n        $len = strlen($s);\n        if($len == ''){\n            return $s;\n        }\n        $origin = $s;\n        $reverse = strrev($s);\n        $arr = [];\n        $maxLen = 0;\n        $maxStart = 0;\n\n        for($i=0; $i&lt;$len; $i++){\/\/\u539f\u59cb\u5b57\u7b26\u4e32\u4e0b\u6807\n            for($j=0; $j&lt;$len; $j++){ \/\/\u5012\u7f6e\u540e\u5b57\u7b26\u4e32\u4e0b\u6807\n                if($origin[$i] == $reverse[$j]){\n                    if($i == 0 || $j ==0){\n                        $arr[$i][$j] = 1;\n                    }else{\n                        \/\/\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\n                        $arr[$i][$j] = $arr[$i-1][$j-1] + 1;\n                    }\n                    \/**\n                     * \u7528\u6765\u89c4\u907f\u7279\u6b8a\u60c5\u51b5\n                     * \u5982\uff1aS = abc435cba\uff0cS' = abc534cba\n                     * \u4e0d\u7279\u6b8a\u5904\u7406\uff0cabc \u4f1a\u88ab\u5224\u65ad\u4e3a\u6700\u5c0f\u56de\u6587\u5b50\u4e32\n                     * \u89e3\uff1a\u5224\u65ad\u5012\u7f6e\u524d\u7684\u5b50\u4e32\u4e0b\u6807\uff0c\u662f\u5426\u7b26\u5408\u6761\u4ef6\n                     *\/\n                    if($arr[$i][$j] &gt; $maxLen){\n                        $beforeJ = $len - $j - 1;\n                        if($beforeJ + $arr[$i][$j] -1 == $i){\n                            $maxLen = $arr[$i][$j];\n                            $maxStart = $i;\n                        }\n                    }\n                }else{\n                    $arr[$i][$j] = 0;\n                }\n            }\n        }\n        echo substr($s,$maxStart-$maxLen+1,$maxLen);\n    }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"item-1-3\">\u4e09\u3001\u4e2d\u5fc3\u62d3\u5c55\u6cd5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u628a\u6bcf\u4e2a\u5b57\u6bcd\u5f53\u6210\u56de\u6587\u4e32\u7684\u4e2d\u5fc3\u3002\u8003\u8651\u4e24\u79cd\u60c5\u51b5\uff0c\u957f\u5ea6\u4e3a\u5947\u6570\u548c\u5076\u6570<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Solution3 {\n\n    \/**\n     * @param String $s\n     * @return String\n     *\/\n    function longestPalindrome($s) {\n        $n = strlen($s);\n        if($n == ''){\n            return $s;\n        }\n        $start = 0;\n        $maxlen = 0;\n\n        for($i=0; $i&lt;$n; $i++){\n            $len1 = $this-&gt;isPalindrome($s,$i,$i);\/\/\u5947\u6570\n            $len2 = $this-&gt;isPalindrome($s,$i,$i+1);\/\/\u5076\u6570\n            $len = max($len1,$len2);\n            if($len &gt; $maxlen){\n                $start = $i - ($len-1)\/2;\n                $maxlen = $len;\n            }\n        }\n        return substr($s,$start,$len) ;\n    }\n\n    function expend($str,$i,$j){\n        $n = strlen($str);\n        $l = $i;\n        $r = $j;\n\n        while($l&gt;=0 &amp;&amp; $r&lt;$n &amp;&amp; $str[$l] == $str[$r]){\n            $l-- ;\n            $r++ ;\n        }\n\n        return $r-$l-1;\n\n    }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"item-1-4\">\u4e09\u3001\u9a6c\u62c9\u8f66\u7b97\u6cd5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u91cd\u65b0\u7ec4\u5408\u5b57\u7b26\u4e32\uff0c\u5c06\u5176\u53d8\u4e3a\u5947\u6570\u4e2a\u3002\u5145\u5206\u7684\u5229\u7528\u56de\u6587\u4e32\u7684\u5bf9\u79f0\u6027\u8d28\uff0c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Solution4 {\n\n    \/**\n     * @param String $s\n     * @return String\n     *\/\n    function longestPalindrome($s) {\n        $T = $this-&gt;malache($s);\n        $n = strlen($T);\n        $C = $R = 0;\n        $p = [];\n        for($i=1; $i&lt;$n-1; $i++){\n            $i_mirror = $C*2 - $i;\n            if($R&gt;$i){\n                $p[$i] = min($R-$i,$p[$i_mirror]);\n            }else{\n                $p[$i] = 0;\n            }\n            while(($T[$i-1-$p[$i]]) == ($T[$i+1+$p[$i]]) ){\n                $p[$i]++;\n            }\n            if($i + $p[$i] &gt; $R) {\n                $C = $i;\n                $R = $i + $p[$i];\n            }\n        }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u957f\u56de\u6587\u5b50\u4e32 \u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32 s\uff0c\u627e\u5230 s \u4e2d\u6700\u957f\u7684\u56de\u6587\u5b50\u4e32&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-221","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/comments?post=221"}],"version-history":[{"count":1,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/221\/revisions"}],"predecessor-version":[{"id":1213,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/221\/revisions\/1213"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}