{"id":2102,"date":"2016-08-14T22:17:07","date_gmt":"2016-08-14T14:17:07","guid":{"rendered":"http:\/\/boweihe.me\/?p=2102"},"modified":"2016-08-14T22:17:07","modified_gmt":"2016-08-14T14:17:07","slug":"leetcode-14-longest-common-prefix","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2102","title":{"rendered":"LeetCode 14. Longest Common Prefix"},"content":{"rendered":"<p>Write a function to find the longest common prefix string amongst an array of strings.<\/p>\n<h3>\u5206\u6790<\/h3>\n<p>\u8fd9\u4e2a\u9898\u76ee\u76f4\u63a5\u626b\u4e00\u904d\u5c31\u884c\u4e86\uff0c\u590d\u6742\u5ea6\u662fO(min(strlen))\uff0cstrlen\u662f\u5b57\u7b26\u4e32\u7684\u957f\u5ea6<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    string longestCommonPrefix(vector&lt;string&gt;&amp; strs) {\n        if(strs.size() == 0)\n            return \"\";\n        else if(strs.size() == 1)\n            return strs[0];\n        int currPos = 0;\n        bool shouldStop = false;\n        while(!shouldStop){\n            char c;\n            for(int row=0; row &lt; strs.size(); row++){\n                if(strs[row].size() == currPos){\n                    shouldStop = true;\/\/Meet the end of some string\n                    break;\n                }\n                if(row == 0){\n                    c = strs[row][currPos];\n                } else {\n                    if(strs[row][currPos] != c){\n                        shouldStop = true;\n                        break;\n                    }\n                }\n            }\n            if(!shouldStop)\n                currPos++;\n        }\n        return strs[0].substr(0, currPos);\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a function to find the longest common prefix string amongst an array of strings. \u5206\u6790 \u8fd9\u4e2a\u9898\u76ee\u76f4\u63a5\u626b\u4e00\u904d\u5c31\u884c\u4e86\uff0c\u590d\u6742\u5ea6\u662fO(min(strlen))\uff0cstrlen\u662f\u5b57\u7b26\u4e32\u7684\u957f\u5ea6 \u4ee3\u7801 class Solution { public: string longestCommonPrefix(vector&lt;string&gt;&amp; strs) { if(strs.size() == 0) return &#8220;&#8221;; else if(strs.size() == 1) return strs[0]; int currPos = 0; bool shouldStop = false; while(!shouldStop){ char c; for(int row=0; row &lt; strs.size(); row++){ if(strs[row].size() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-2102","post","type-post","status-publish","format-standard","hentry","category-technical"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2102"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2102\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}