{"id":953,"date":"2017-05-07T21:54:43","date_gmt":"2017-05-07T13:54:43","guid":{"rendered":"http:\/\/www.mrtblog.cn\/?p=953"},"modified":"2023-03-05T17:07:36","modified_gmt":"2023-03-05T09:07:36","slug":"%e3%80%90c%e3%80%91%e3%80%90leetcode%e3%80%912020-4-302020-5-6","status":"publish","type":"post","link":"http:\/\/www.mrtblog.cn\/?p=953","title":{"rendered":"\u3010C++\u3011\u3010LeetCode\u30112020.4.30~2020.5.7"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 1,614<\/span><span class='epvc-label'> Views<\/span><\/div><p><em><strong>10. Regular Expression Matching<\/strong><\/em><br \/>\nGiven an input string (s) and a pattern (p), implement regular expression matching with support for &#8216;.&#8217; and &#8216;*&#8217;.<\/p>\n<p>&#8216;.&#8217; Matches any single character.<br \/>\n&#8216;*&#8217; Matches zero or more of the preceding element.<br \/>\nThe matching should cover the entire input string (not partial).<\/p>\n<p>Note:<\/p>\n<p>s could be empty and contains only lowercase letters a-z.<br \/>\np could be empty and contains only lowercase letters a-z, and characters like . or *.<\/p>\n<p><strong>Example 1:<\/strong><br \/>\nInput:<br \/>\ns = &#8220;aa&#8221;<br \/>\np = &#8220;a&#8221;<br \/>\nOutput: false<br \/>\nExplanation: &#8220;a&#8221; does not match the entire string &#8220;aa&#8221;.<\/p>\n<p><strong>Example 2:<\/strong><br \/>\nInput:<br \/>\ns = &#8220;aa&#8221;<br \/>\np = &#8220;a*&#8221;<br \/>\nOutput: true<br \/>\nExplanation: &#8216;*&#8217; means zero or more of the preceding element, &#8216;a&#8217;. Therefore, by repeating &#8216;a&#8217; once, it becomes &#8220;aa&#8221;.<\/p>\n<p><strong>Example 3:<\/strong><br \/>\nInput:<br \/>\ns = &#8220;ab&#8221;<br \/>\np = &#8220;.*&#8221;<br \/>\nOutput: true<br \/>\nExplanation: &#8220;.*&#8221; means &#8220;zero or more (*) of any character (.)&#8221;.<\/p>\n<p><strong>Example 4:<\/strong><br \/>\nInput:<br \/>\ns = &#8220;aab&#8221;<br \/>\np = &#8220;c*a*b&#8221;<br \/>\nOutput: true<br \/>\nExplanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches &#8220;aab&#8221;.<\/p>\n<p><strong>Example 5:<\/strong><br \/>\nInput:<br \/>\ns = &#8220;mississippi&#8221;<br \/>\np = &#8220;mis*is*p*.&#8221;<br \/>\nOutput: false<\/p>\n<pre class=\"highlight\"><code class=\"language-cpp line-numbers\">\nclass Solution {\npublic:\n\/*\ns: adadasadad\np: **ada**.*ad\n\u4e0b\u4e00\u4f4d\u662f*\n\u5f53\u524d\u4f4d\u4e0d\u80fd\u5339\u914d\n*\u5f53\u505a0\u5904\u7406\n\u5f53\u524d\u4f4d\u80fd\u5339\u914d\n\u548cs\u7684\u4e0b\u4e00\u4f4d\u7ee7\u7eed\u6bd4\u8f83\n\u5982\u679c\u51fa\u73b0\u5339\u914d\u4e0d\u4e0a\uff0cp\u5411\u540e\u8df32\u4f4d\n\u4e0b\u4e00\u4f4d\u4e0d\u662f*\n\u5f53\u524ds\u548cp\u6bd4\u8f83\n*\/\n    bool isMatch(string&amp; s, string&amp; p, int i, int j) {\n        if (j == p.size()) {\n            \/\/p\u5df2\u7ecf\u5339\u914d\u5b8c\u4e86\n            return (i == s.size());\n        }\n        \/\/j\u4e0d\u5230\u672b\u5c3e\n        if (j == p.size() - 1 || (j &lt; p.size() - 1 &amp;&amp; p[j+1] != &#039;*&#039;)) {\n            \/\/p\u5df2\u7ecf\u5230\u4e86\u6700\u540e\u4e00\u4f4d\u6216\u8005\u4e0b\u4e00\u4f4d\u4e0d\u4e3a*,\u5f53\u524d\u5b57\u7b26\u5fc5\u987b\u5339\u914d\n            if (i &lt; s.size() &amp;&amp; (s[i] == p [j] || p[j] == &#039;.&#039;)) {\n                return isMatch(s, p, i+1, j+1);\n            }\n            return false;\n        }\n        \/\/j\u4e0b\u4e00\u4f4d\u4e3a*\u4e14\u4e0d\u5728\u672b\u5c3e\n        while (i &lt; s.size()) {\n            if (s[i] == p [j] || p[j] == &#039;.&#039;) {\n                \/\/\u5f53\u524d\u4f4d\u5339\u914d,\u8df3\u4e24\u4f4d\u770b\u770b\u884c\u4e0d\u884c\n                if (isMatch(s, p, i, j+2)) {\n                    return true;\n                }\n                \/\/\u8df3\u4e0d\u4e86,s\u8df31\u4f4d\n                i++;\n            } else {\n                \/\/\u5f53\u524d\u4f4d\u4e0d\u5339\u914d,\u53ea\u80fd\u8df3\u4e24\u4f4d\n                return isMatch(s, p, i, j+2);\n            }\n        }\n        \/\/\u8bf4\u660es\u5230\u672b\u5c3e\u4e86\uff0c\u8df3\u8fc7p\u7684\u4e24\u4f4d\u770b\u770b\u6709\u6ca1\u6709\u5269\u4f59\n        return isMatch(s, p, i, j+2);\n    }\n\n    bool isMatch(string s, string p) {\n        return isMatch(s, p, 0, 0);\n    }\n};\n<\/code><\/pre>\n<hr>\n<p><em><strong>11. Container With Most Water<\/strong><\/em><br \/>\nGiven n non-negative integers a1, a2, &#8230;, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.<\/p>\n<p>Note: You may not slant the container and n is at least 2.<\/p>\n<p><strong>Example :<\/strong><br \/>\nInput: [1,8,6,2,5,4,8,3,7]<br \/>\nOutput: 49<\/p>\n<pre class=\"highlight\"><code class=\"language-cpp line-numbers\">\nclass Solution {\npublic:\n\/*\n\u5982\u679c\u4e24\u8fb9\u5411\u4e2d\u95f4\u9760\uff0c\u6709\u6bd4\u4e24\u8fb9\u9762\u79ef\u66f4\u5927\u7684\u67f1\u5b50\uff0c\u5219\u4e00\u5b9a\u6709\u4e00\u67f1\u6bd4\u4e24\u8fb9\u7684\u9ad8\n\u6bcf\u6b21\u4e00\u5b9a\u662f\u4ece\u8f83\u4f4e\u7684\u67f1\u5b50\u5f00\u59cb\u5411\u4e2d\u95f4\u9760\u62e2\n*\/\n    int CalScore(vector&lt;int&gt;&amp; height, int l, int r) {\n        if (l == -1 || r == -1) {\n            return 0;\n        }\n        int Val = height[l] &lt; height[r] ? height[l] : height[r];\n        return Val * (r - l);\n    }\n    int maxArea(vector&lt;int&gt;&amp; height) {\n        if (height.size() &lt; 2) {\n            return 0;\n        }\n        int l = 0;\n        int r = height.size() - 1;\n        int res = 0;\n        while (l &lt; r) {\n            int iScore = CalScore(height, l, r);\n            res = (iScore &gt; res) ? iScore : res;\n\n            if (height[l] &gt; height[r]) {\n                r--;\n            } else {\n                l++;\n            }\n        }\n        return res;\n    }\n};\n<\/code><\/pre>\n<hr>\n<p><em><strong>12. Integer to Roman<\/strong><\/em><\/p>\n<p>Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.<\/p>\n<p>Symbol       Value<br \/>\nI             1<br \/>\nV             5<br \/>\nX             10<br \/>\nL             50<br \/>\nC             100<br \/>\nD             500<br \/>\nM             1000<br \/>\nFor example, two is written as II in Roman numeral, just two one&#8217;s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.<\/p>\n<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:<\/p>\n<p>I can be placed before V (5) and X (10) to make 4 and 9.<br \/>\nX can be placed before L (50) and C (100) to make 40 and 90.<br \/>\nC can be placed before D (500) and M (1000) to make 400 and 900.<br \/>\nGiven an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.<\/p>\n<p><strong>Example 1:<\/strong><br \/>\nInput: 3<br \/>\nOutput: &#8220;III&#8221;<\/p>\n<p><strong>Example 2:<\/strong><br \/>\nInput: 4<br \/>\nOutput: &#8220;IV&#8221;<\/p>\n<p><strong>Example 3:<\/strong><br \/>\nInput: 9<br \/>\nOutput: &#8220;IX&#8221;<\/p>\n<p><strong>Example 4:<\/strong><br \/>\nInput: 58<br \/>\nOutput: &#8220;LVIII&#8221;<br \/>\nExplanation: L = 50, V = 5, III = 3.<\/p>\n<p><strong>Example 5:<\/strong><br \/>\nInput: 1994<br \/>\nOutput: &#8220;MCMXCIV&#8221;<br \/>\nExplanation: M = 1000, CM = 900, XC = 90 and IV = 4.<\/p>\n<pre class=\"highlight\"><code class=\"language-cpp line-numbers\">\nclass Solution {\npublic:\n\/*\n900 CM\n400 CD\n90  XC\n40  XL\n9   IX\n4   IV\n*\/\n    const int Count = 13;\n    string intToRoman(int num) {\n        string res = &quot;&quot;;\n        string strs[] = {&quot;M&quot;,&quot;CM&quot;,&quot;D&quot;,&quot;CD&quot;,&quot;C&quot;,&quot;XC&quot;,&quot;L&quot;,&quot;XL&quot;,&quot;X&quot;,&quot;IX&quot;,&quot;V&quot;,&quot;IV&quot;,&quot;I&quot;};\n        int nums[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};\n        for (int i = 0; i &lt; Count; ++i) {\n            while (num &gt;= nums[i]) {\n                res += strs[i];\n                num -= nums[i];\n            }\n        }\n        return res;\n    }\n};\n<\/code><\/pre>\n<hr>\n<p><em><strong>13. Roman to Integer<\/strong><\/em><br \/>\nRoman numerals are represented by seven different symbols: I, V, X, L, C, D and M.<\/p>\n<p>Symbol       Value<br \/>\nI             1<br \/>\nV             5<br \/>\nX             10<br \/>\nL             50<br \/>\nC             100<br \/>\nD             500<br \/>\nM             1000<br \/>\nFor example, two is written as II in Roman numeral, just two one&#8217;s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.<\/p>\n<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:<\/p>\n<p>I can be placed before V (5) and X (10) to make 4 and 9.<br \/>\nX can be placed before L (50) and C (100) to make 40 and 90.<br \/>\nC can be placed before D (500) and M (1000) to make 400 and 900.<br \/>\nGiven a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.<\/p>\n<p><strong>Example 1:<\/strong><br \/>\nInput: &#8220;III&#8221;<br \/>\nOutput: 3<\/p>\n<p><strong>Example 2:<\/strong><br \/>\nInput: &#8220;IV&#8221;<br \/>\nOutput: 4<\/p>\n<p><strong>Example 3:<\/strong><br \/>\nInput: &#8220;IX&#8221;<br \/>\nOutput: 9<\/p>\n<p><strong>Example 4:<\/strong><br \/>\nInput: &#8220;LVIII&#8221;<br \/>\nOutput: 58<br \/>\nExplanation: L = 50, V= 5, III = 3.<\/p>\n<p><strong>Example 5:<\/strong><br \/>\nInput: &#8220;MCMXCIV&#8221;<br \/>\nOutput: 1994<br \/>\nExplanation: M = 1000, CM = 900, XC = 90 and IV = 4.<\/p>\n<pre class=\"highlight\"><code class=\"language-cpp line-numbers\">\nclass Solution {\npublic:\n    int GetNum(string* strs, int* nums, char c) {\n        string ss(1, c);\n        for (size_t k = 0; k &lt; 7; ++k) {\n            if (strs[k] == ss) {\n                return nums[k];\n            }\n        }\n        return -1;\n    }\n    int romanToInt(string s) {\n        string strs[] = {&quot;M&quot;,&quot;D&quot;,&quot;C&quot;,&quot;L&quot;,&quot;X&quot;,&quot;V&quot;,&quot;I&quot;};\n        int nums[] = {1000,500,100,50,10,5,1};\n        int res = GetNum(strs, nums, s[0]);\n        for (size_t i = 1; i &lt; s.size(); ++i) {\n            int l1 = GetNum(strs, nums, s[i - 1]);\n            int l2 = GetNum(strs, nums, s[i]);\n\n            res += l2;\n            if (l1 &lt; l2) {\n                res -= 2 * l1;\n            }\n        }\n        return res;\n    }\n};\n<\/code><\/pre>\n<hr>\n<p><em><strong>14. Longest Common Prefix<\/strong><\/em><br \/>\nWrite a function to find the longest common prefix string amongst an array of strings.<\/p>\n<p>If there is no common prefix, return an empty string &#8220;&#8221;.<\/p>\n<p><strong>Example 1:<\/strong><br \/>\nInput: [&#8220;flower&#8221;,&#8221;flow&#8221;,&#8221;flight&#8221;]<br \/>\nOutput: &#8220;fl&#8221;<\/p>\n<p><strong>Example 2:<\/strong><br \/>\nInput: [&#8220;dog&#8221;,&#8221;racecar&#8221;,&#8221;car&#8221;]<br \/>\nOutput: &#8220;&#8221;<br \/>\nExplanation: There is no common prefix among the input strings.<br \/>\nNote:<\/p>\n<p>All given inputs are in lowercase letters a-z.<\/p>\n<pre class=\"highlight\"><code class=\"language-cpp line-numbers\">\nclass Solution {\npublic:\n    string longestCommonPrefix(vector&lt;string&gt;&amp; strs) {\n        int minLen = 99999;\n        for (size_t i = 0; i &lt; strs.size(); ++i) {\n            if (strs[i].size() &lt; minLen) {\n                minLen = strs[i].size();\n            }\n        }\n        if (minLen == 99999 || strs.size() &lt; 1) {\n            return &quot;&quot;;\n        }\n        size_t i = 0;\n        for (; i &lt; minLen; ++i) {\n            char c = strs[0][i];\n            for (size_t j = 1; j &lt; strs.size(); ++j) {\n                if (strs[j][i] != c) {\n                    return strs[0].substr(0, i);\n                }\n            }\n        }\n        return strs[0].substr(0, i);\n    }\n};\n<\/code><\/pre>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>1,614 Views10. Regular Expression Matching Given an inp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-953","post","type-post","status-publish","format-standard","hentry","category-lc"],"_links":{"self":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=953"}],"version-history":[{"count":5,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/953\/revisions"}],"predecessor-version":[{"id":1010,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/953\/revisions\/1010"}],"wp:attachment":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=953"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}