{"id":1529,"date":"2022-01-07T21:28:55","date_gmt":"2022-01-07T13:28:55","guid":{"rendered":"http:\/\/139.9.1.231\/?p=1529"},"modified":"2022-01-08T09:53:26","modified_gmt":"2022-01-08T01:53:26","slug":"leetcodeday3","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/07\/leetcodeday3\/","title":{"rendered":"leetcodeday3&#8212;\u65e0\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32"},"content":{"rendered":"\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32&nbsp;<code>s<\/code>&nbsp;\uff0c\u8bf7\u4f60\u627e\u51fa\u5176\u4e2d\u4e0d\u542b\u6709\u91cd\u590d\u5b57\u7b26\u7684&nbsp;<strong>\u6700\u957f\u5b50\u4e32&nbsp;<\/strong>\u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165: <\/strong>s = \"abcabcbb\"\n<strong>\u8f93\u51fa: <\/strong>3 \n<strong>\u89e3\u91ca:<\/strong> \u56e0\u4e3a\u65e0\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\u662f \"abc\"\uff0c\u6240\u4ee5\u5176\u957f\u5ea6\u4e3a 3\u3002<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165: <\/strong>s = \"bbbbb\"\n<strong>\u8f93\u51fa: <\/strong>1\n<strong>\u89e3\u91ca: <\/strong>\u56e0\u4e3a\u65e0\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\u662f \"b\"\uff0c\u6240\u4ee5\u5176\u957f\u5ea6\u4e3a 1\u3002<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165: <\/strong>s = \"pwwkew\"\n<strong>\u8f93\u51fa: <\/strong>3\n<strong>\u89e3\u91ca: <\/strong>\u56e0\u4e3a\u65e0\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\u662f&nbsp;\"wke\"\uff0c\u6240\u4ee5\u5176\u957f\u5ea6\u4e3a 3\u3002\n&nbsp;    \u8bf7\u6ce8\u610f\uff0c\u4f60\u7684\u7b54\u6848\u5fc5\u987b\u662f <strong>\u5b50\u4e32 <\/strong>\u7684\u957f\u5ea6\uff0c\"pwke\"&nbsp;\u662f\u4e00\u4e2a<em>\u5b50\u5e8f\u5217\uff0c<\/em>\u4e0d\u662f\u5b50\u4e32\u3002\n\n\u793a\u4f8b 4:\n\n\u8f93\u5165: s = \"\"\n\u8f93\u51fa: 0<\/code><\/pre>\n\n\n\n<p><strong>\u601d\u8def\uff1a<\/strong><\/p>\n\n\n\n<p>\u6211\u7b2c\u4e00\u904d\u5199\u7684\u65f6\u5019\uff0c\u60f3\u6cd5\u662f\u628a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u4e00\u4e2a\u961f\u5217\u8f93\u5165\uff0c\u5f53\u524d\u8f93\u5165\u5230\u961f\u5217 \u4e2d\u7684j in \u961f\u5217\u65f6\uff0c\u5c31\u628a\u961f\u5217\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u5217\uff0c\u5e76\u66f4\u65b0maxlenght\u3002\u4ee3\u7801\u5982\u4e0b \uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc code=start\nclass Solution:\n    def lengthOfLongestSubstring(self, x: str) -&gt; int:\n        maxlength = 0\n        lens = len(x)\n        for i in range(lens-1):\n            for j in range(i+1,lens):\n                if  x&#91;j] in x&#91;i:j] or  x&#91;j]==x&#91;i]:\n                    maxlength = maxlength if maxlength&gt;(j-i) else (j-i)\n                    break\n                elif j == lens-1:\n                    maxlength = maxlength if maxlength&gt;(j-i+1) else (j-i+1)\n                    break\n                else:\n                    continue\n        return maxlength\n\n# @lc code=end<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u901a\u8fc7\u7387\u4e0d\u662f100\uff0c\u56e0\u4e3a\u6ca1\u6709\u8003\u8651\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h2>Wrong Answer<\/h2>\n\n\n\n<ul><li>879\/987 cases passed (N\/A)<\/li><\/ul>\n\n\n\n<h3>Testcase<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\" \"<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u5728\u5f00\u59cb\u52a0\u4e00\u4e2a\u5224\u65ad if  &#8221; &#8221; in string <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def lengthOfLongestSubstring(self, x: str) -&gt; int:\n        maxlength = 0\n        lens = len(x)\n        if \" \" in x&#91;0:1]  or lens==1:\n            return 1\n        for i in range(lens-1):\n            for j in range(i+1,lens):\n                if  x&#91;j] in x&#91;i:j] or  x&#91;j]==x&#91;i]:\n                    maxlength = maxlength if maxlength&gt;(j-i) else (j-i)\n                    break\n                elif j == lens-1:\n                    maxlength = maxlength if maxlength&gt;(j-i+1) else (j-i+1)\n                    break\n                else:\n                    continue\n        return maxlength\n<\/code><\/pre>\n\n\n\n<h2>Accepted<\/h2>\n\n\n\n<ul><li>987\/987 cases passed (520 ms)<\/li><li>Your runtime beats 8.75 % of python3 submissions<\/li><li>Your memory usage beats 16.81 % of python3 submissions (15.2 MB)<\/li><\/ul>\n\n\n\n<p>\u6539\u8fdb\u7248\uff1a<\/p>\n\n\n\n<p>\u6ed1\u52a8\u7a97\u53e3<br>\u601d\u8def\u548c\u7b97\u6cd5<\/p>\n\n\n\n<p>\u6211\u4eec\u5148\u7528\u4e00\u4e2a\u4f8b\u5b50\u8003\u8651\u5982\u4f55\u5728\u8f83\u4f18\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5185\u901a\u8fc7\u672c\u9898\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u4e0d\u59a8\u4ee5\u793a\u4f8b\u4e00\u4e2d\u7684\u5b57\u7b26\u4e32 \\texttt{abcabcbb}abcabcbb \u4e3a\u4f8b\uff0c\u627e\u51fa\u4ece\u6bcf\u4e00\u4e2a\u5b57\u7b26\u5f00\u59cb\u7684\uff0c\u4e0d\u5305\u542b\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\uff0c\u90a3\u4e48\u5176\u4e2d\u6700\u957f\u7684\u90a3\u4e2a\u5b57\u7b26\u4e32\u5373\u4e3a\u7b54\u6848\u3002\u5bf9\u4e8e\u793a\u4f8b\u4e00\u4e2d\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u5217\u4e3e\u51fa\u8fd9\u4e9b\u7ed3\u679c\uff0c\u5176\u4e2d\u62ec\u53f7\u4e2d\u8868\u793a\u9009\u4e2d\u7684\u5b57\u7b26\u4ee5\u53ca\u6700\u957f\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n\n\n\n<p>\u4ee5 \\(\\texttt{(a)bcabcbb}(a)bcabcbb\\) \u5f00\u59cb\u7684\u6700\u957f\u5b57\u7b26\u4e32\u4e3a \\(\\texttt{(abc)abcbb}(abc)abcbb\\)\uff1b<br>\u4ee5 \\(\\texttt{a(b)cabcbb}a(b)cabcbb\\) \u5f00\u59cb\u7684\u6700\u957f\u5b57\u7b26\u4e32\u4e3a \\(\\texttt{a(bca)bcbb}a(bca)bcbb\\)\uff1b<br>\u4ee5 \\(\\texttt{ab(c)abcbb}ab(c)abcbb\\) \u5f00\u59cb\u7684\u6700\u957f\u5b57\u7b26\u4e32\u4e3a \\(\\texttt{ab(cab)cbb}ab(cab)cbb\\)\uff1b<br>\u4ee5  \\(\\texttt{abc(a)bcbb}abc(a)bcbb\\) \u5f00\u59cb\u7684\u6700\u957f\u5b57\u7b26\u4e32\u4e3a  \\(\\texttt{abc(abc)bb}abc(abc)bb\\)\uff1b<br>\u4ee5  \\(\\texttt{abca(b)cbb}abca(b)cbb\\) \u5f00\u59cb\u7684\u6700\u957f\u5b57\u7b26\u4e32\u4e3a  \\(\\texttt{abca(bc)bb}abca(bc)bb\\)\uff1b<br>\u4ee5  \\(\\texttt{abcab(c)bb}abcab(c)bb\\) \u5f00\u59cb\u7684\u6700\u957f\u5b57\u7b26\u4e32\u4e3a  \\(\\texttt{abcab(cb)b}abcab(cb)b\\)\uff1b<br>\u4ee5  \\(\\texttt{abcabc(b)b}abcabc(b)b\\) \u5f00\u59cb\u7684\u6700\u957f\u5b57\u7b26\u4e32\u4e3a\\(\\texttt{abcabc(b)b}abcabc(b)b\\)\uff1b<br>\u4ee5  \\(\\texttt{abcabcb(b)}abcabcb(b)\\) \u5f00\u59cb\u7684\u6700\u957f\u5b57\u7b26\u4e32\u4e3a \\(\\texttt{abcabcb(b)}abcabcb(b)\\)\u3002<br>\u53d1\u73b0\u4e86\u4ec0\u4e48\uff1f\u5982\u679c\u6211\u4eec\u4f9d\u6b21\u9012\u589e\u5730\u679a\u4e3e\u5b50\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u90a3\u4e48\u5b50\u4e32\u7684\u7ed3\u675f\u4f4d\u7f6e\u4e5f\u662f\u9012\u589e\u7684\uff01\u8fd9\u91cc\u7684\u539f\u56e0\u5728\u4e8e\uff0c\u5047\u8bbe\u6211\u4eec\u9009\u62e9\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c k\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u8d77\u59cb\u4f4d\u7f6e\uff0c\u5e76\u4e14\u5f97\u5230\u4e86\u4e0d\u5305\u542b\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\u7684\u7ed3\u675f\u4f4d\u7f6e\u4e3a r_k\u3002\u90a3\u4e48\u5f53\u6211\u4eec\u9009\u62e9\u7b2c k+1 \u4e2a\u5b57\u7b26\u4f5c\u4e3a\u8d77\u59cb\u4f4d\u7f6e\u65f6\uff0c\u9996\u5148\u4ece k+1 \u5230 r_k\u7684\u5b57\u7b26\u663e\u7136\u662f\u4e0d\u91cd\u590d\u7684\uff0c\u5e76\u4e14\u7531\u4e8e\u5c11\u4e86\u539f\u672c\u7684\u7b2c k\u4e2a\u5b57\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u7ee7\u7eed\u589e\u5927 r_k\uff0c\u76f4\u5230\u53f3\u4fa7\u51fa\u73b0\u4e86\u91cd\u590d\u5b57\u7b26\u4e3a\u6b62\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528\u300c\u6ed1\u52a8\u7a97\u53e3\u300d\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u4e86\uff1a<\/p>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528\u4e24\u4e2a\u6307\u9488\u8868\u793a\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e2a\u5b50\u4e32\uff08\u6216\u7a97\u53e3\uff09\u7684\u5de6\u53f3\u8fb9\u754c\uff0c\u5176\u4e2d\u5de6\u6307\u9488\u4ee3\u8868\u7740\u4e0a\u6587\u4e2d\u300c\u679a\u4e3e\u5b50\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u300d\uff0c\u800c\u53f3\u6307\u9488\u5373\u4e3a\u4e0a\u6587\u4e2d\u7684 r_k\u5728\u6bcf\u4e00\u6b65\u7684\u64cd\u4f5c\u4e2d\uff0c\u6211\u4eec\u4f1a\u5c06\u5de6\u6307\u9488\u5411\u53f3\u79fb\u52a8\u4e00\u683c\uff0c\u8868\u793a\u6211\u4eec\u5f00\u59cb\u679a\u4e3e\u4e0b\u4e00\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u8d77\u59cb\u4f4d\u7f6e\uff0c\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u4e0d\u65ad\u5730\u5411\u53f3\u79fb\u52a8\u53f3\u6307\u9488\uff0c\u4f46\u9700\u8981\u4fdd\u8bc1\u8fd9\u4e24\u4e2a\u6307\u9488\u5bf9\u5e94\u7684\u5b50\u4e32\u4e2d\u6ca1\u6709\u91cd\u590d\u7684\u5b57\u7b26\u3002\u5728\u79fb\u52a8\u7ed3\u675f\u540e\uff0c\u8fd9\u4e2a\u5b50\u4e32\u5c31\u5bf9\u5e94\u7740\u4ee5\u5de6\u6307\u9488\u5f00\u59cb\u7684\uff0c\u4e0d\u5305\u542b\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\u3002\u6211\u4eec\u8bb0\u5f55\u4e0b\u8fd9\u4e2a\u5b50\u4e32\u7684\u957f\u5ea6\uff1b<\/p>\n\n\n\n<p>\u5728\u679a\u4e3e\u7ed3\u675f\u540e\uff0c\u6211\u4eec\u627e\u5230\u7684\u6700\u957f\u7684\u5b50\u4e32\u7684\u957f\u5ea6\u5373\u4e3a\u7b54\u6848<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def lengthOfLongestSubstring(self, s: str) -&gt; int:\n        # \u54c8\u5e0c\u96c6\u5408\uff0c\u8bb0\u5f55\u6bcf\u4e2a\u5b57\u7b26\u662f\u5426\u51fa\u73b0\u8fc7\n        occ = set()\n        n = len(s)\n        # \u53f3\u6307\u9488\uff0c\u521d\u59cb\u503c\u4e3a -1\uff0c\u76f8\u5f53\u4e8e\u6211\u4eec\u5728\u5b57\u7b26\u4e32\u7684\u5de6\u8fb9\u754c\u7684\u5de6\u4fa7\uff0c\u8fd8\u6ca1\u6709\u5f00\u59cb\u79fb\u52a8\n        rk, ans = -1, 0\n        for i in range(n):\n            if i != 0:\n                # \u5de6\u6307\u9488\u5411\u53f3\u79fb\u52a8\u4e00\u683c\uff0c\u79fb\u9664\u4e00\u4e2a\u5b57\u7b26\n                occ.remove(s&#91;i - 1])\n            while rk + 1 &lt; n and s&#91;rk + 1] not in occ:\n                # \u4e0d\u65ad\u5730\u79fb\u52a8\u53f3\u6307\u9488\n                occ.add(s&#91;rk + 1])\n                rk += 1\n            # \u7b2c i \u5230 rk \u4e2a\u5b57\u7b26\u662f\u4e00\u4e2a\u6781\u957f\u7684\u65e0\u91cd\u590d\u5b57\u7b26\u5b50\u4e32\n            ans = max(ans, rk - i + 1)\n        return ans\n\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" width=\"1024\" height=\"259\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-143-1024x259.png\" alt=\"\" class=\"wp-image-1538\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-143-1024x259.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-143-300x76.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-143-768x194.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-143.png 1067w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul><li>\u54c8\u5e0cMap \u53ea\u9700\u4e00\u6b21\u904d\u5386, more efficiency<\/li><li>\u9644<code>&nbsp;Python<\/code>&nbsp;\u4ee3\u7801<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def lengthOfLongestSubstring(self, s: str) -&gt; int:\n        k, res, c_dict = -1, 0, {}\n        for i, c in enumerate(s):\n            if c in c_dict and c_dict&#91;c] &gt; k:  # \u5b57\u7b26c\u5728\u5b57\u5178\u4e2d \u4e14 \u4e0a\u6b21\u51fa\u73b0\u7684\u4e0b\u6807\u5927\u4e8e\u5f53\u524d\u957f\u5ea6\u7684\u8d77\u59cb\u4e0b\u6807\n                k = c_dict&#91;c]\n                c_dict&#91;c] = i\n            else:\n                c_dict&#91;c] = i\n                res = max(res, i-k)\n        return res<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32&nbsp;s&nbsp;\uff0c\u8bf7\u4f60\u627e\u51fa\u5176\u4e2d\u4e0d\u542b\u6709\u91cd\u590d\u5b57\u7b26\u7684&nbsp;\u6700\u957f\u5b50\u4e32&nbsp;\u7684\u957f\u5ea6\u3002 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/07\/leetcodeday3\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday3&#8212;\u65e0\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32<\/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\/1529"}],"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=1529"}],"version-history":[{"count":19,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1529\/revisions"}],"predecessor-version":[{"id":1554,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1529\/revisions\/1554"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=1529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=1529"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=1529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}