{"id":1822,"date":"2022-01-11T17:15:59","date_gmt":"2022-01-11T09:15:59","guid":{"rendered":"http:\/\/139.9.1.231\/?p=1822"},"modified":"2022-01-12T10:19:46","modified_gmt":"2022-01-12T02:19:46","slug":"leetcodeday14-2","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/11\/leetcodeday14-2\/","title":{"rendered":"leetcodeday14 &#8212;\u6700\u957f\u516c\u5171\u524d\u7f00"},"content":{"rendered":"\n<p>\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u67e5\u627e\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u6700\u957f\u516c\u5171\u524d\u7f00\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4e0d\u5b58\u5728\u516c\u5171\u524d\u7f00\uff0c\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32&nbsp;<code>\"\"<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>strs = &#91;\"flower\",\"flow\",\"flight\"]\n<strong>\u8f93\u51fa\uff1a<\/strong>\"fl\"<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>strs = &#91;\"dog\",\"racecar\",\"car\"]\n<strong>\u8f93\u51fa\uff1a<\/strong>\"\"\n<strong>\u89e3\u91ca\uff1a<\/strong>\u8f93\u5165\u4e0d\u5b58\u5728\u516c\u5171\u524d\u7f00\u3002<\/code><\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>1 &lt;= strs.length &lt;= 200<\/code><\/li><li><code>0 &lt;= strs[i].length &lt;= 200<\/code><\/li><li><code>strs[i]<\/code>&nbsp;\u4ec5\u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210<\/li><\/ul>\n\n\n\n<p>\u7b2c\u4e00\u6b21\u89e3\u6cd5\uff1a\uff08\u66b4\u529b\uff09\u4f9d\u6b21\u6bd4\u8f83\u6bcf\u4e2astr\u7684\u5143\u7d20<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc code=start\nclass Solution:\n    def longestCommonPrefix(self, strs: List&#91;str]) -&gt; str:\n        lens=len(strs&#91;0])\n        lenlist=len(strs)\n        j=0\n        if lenlist==1 or strs&#91;0]==\"\":\n            return strs&#91;0]\n        while j&lt;lens:\n         \n            for i in range(1,lenlist):\n                fist=strs&#91;0]&#91;j]\n                if j&gt;=len(strs&#91;i]) or strs&#91;i]&#91;j] != fist:\n                    return  strs&#91;0]&#91;0:j]\n            j=j+1\n        return strs&#91;0]&#91;0:j]<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-183.png\" alt=\"\" class=\"wp-image-1826\" width=\"446\" height=\"168\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-183.png 592w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-183-300x113.png 300w\" sizes=\"(max-width: 446px) 100vw, 446px\" \/><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"802\" height=\"265\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-185.png\" alt=\"\" class=\"wp-image-1837\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-185.png 802w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-185-300x99.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-185-768x254.png 768w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"572\" height=\"335\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-186.png\" alt=\"\" class=\"wp-image-1838\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-186.png 572w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-186-300x176.png 300w\" sizes=\"(max-width: 572px) 100vw, 572px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n#\u9012\u5f52\u51fd\u6570\nclass Solution:\n    def longestCommonPrefix(self, strs: List&#91;str]) -> str:\n        def lcp(start, end):\n            if start == end:\n                return strs&#91;start]\n\n            mid = (start + end) \/\/ 2\n            lcpLeft, lcpRight = lcp(start, mid), lcp(mid + 1, end)\n            minLength = min(len(lcpLeft), len(lcpRight))\n            for i in range(minLength):\n                if lcpLeft&#91;i] != lcpRight&#91;i]:\n                    return lcpLeft&#91;:i]\n\n            return lcpLeft&#91;:minLength]\n\n        return \"\" if not strs else lcp(0, len(strs) - 1)\n\n\u4f5c\u8005\uff1aLeetCode-Solution\n\u94fe\u63a5\uff1ahttps:\/\/leetcode-cn.com\/problems\/longest-common-prefix\/solution\/zui-chang-gong-gong-qian-zhui-by-leetcode-solution\/\n\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09\n\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"829\" height=\"198\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-187.png\" alt=\"\" class=\"wp-image-1840\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-187.png 829w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-187-300x72.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-187-768x183.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-187-825x198.png 825w\" sizes=\"(max-width: 829px) 100vw, 829px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-188.png\" alt=\"\" class=\"wp-image-1842\" width=\"281\" height=\"380\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-188.png 410w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-188-222x300.png 222w\" sizes=\"(max-width: 281px) 100vw, 281px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def longestCommonPrefix(self, strs: List&#91;str]) -> str:\n        def isCommonPrefix(length):\n            str0, count = strs&#91;0]&#91;:length], len(strs)\n            return all(strs&#91;i]&#91;:length] == str0 for i in range(1, count))\n\n        if not strs:\n            return \"\"\n\n        minLength = min(len(s) for s in strs)\n        low, high = 0, minLength\n        while low &lt; high:\n            mid = (high - low + 1) \/\/ 2 + low\n            if isCommonPrefix(mid):\n                low = mid\n            else:\n                high = mid - 1\n\n        return strs&#91;0]&#91;:low]\n\n\u4f5c\u8005\uff1aLeetCode-Solution\n\u94fe\u63a5\uff1ahttps:\/\/leetcode-cn.com\/problems\/longest-common-prefix\/solution\/zui-chang-gong-gong-qian-zhui-by-leetcode-solution\/\n\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09\n\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u67e5\u627e\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u6700\u957f\u516c\u5171\u524d\u7f00\u3002 \u5982\u679c\u4e0d\u5b58\u5728\u516c\u5171\u524d\u7f00\uff0c\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32&nbsp;&#8220;&#8221;\u3002 \u793a\u4f8b 1\uff1a &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/11\/leetcodeday14-2\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday14 &#8212;\u6700\u957f\u516c\u5171\u524d\u7f00<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1822"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=1822"}],"version-history":[{"count":6,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1822\/revisions"}],"predecessor-version":[{"id":1843,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1822\/revisions\/1843"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=1822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=1822"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=1822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}