{"id":9953,"date":"2023-03-31T22:54:35","date_gmt":"2023-03-31T14:54:35","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=9953"},"modified":"2024-05-06T07:58:49","modified_gmt":"2024-05-05T23:58:49","slug":"%e6%97%a0%e9%87%8d%e5%a4%8d%e5%ad%97%e7%ac%a6%e7%9a%84%e6%9c%80%e9%95%bf%e5%ad%90%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=9953","title":{"rendered":"\u65e0\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=9953\/#%E9%A2%98%E7%9B%AE\" >\u9898\u76ee<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=9953\/#%E8%A7%A3%E9%A2%98\" >\u89e3\u9898<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.iyatt.com\/?p=9953\/#%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3\" >\u6ed1\u52a8\u7a97\u53e3<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.iyatt.com\/?p=9953\/#%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3_%E5%93%88%E5%B8%8C%E8%A1%A8\" >\u6ed1\u52a8\u7a97\u53e3 + \u54c8\u5e0c\u8868<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%E9%A2%98%E7%9B%AE\"><\/span>\u9898\u76ee<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32 s \uff0c\u8bf7\u4f60\u627e\u51fa\u5176\u4e2d\u4e0d\u542b\u6709\u91cd\u590d\u5b57\u7b26\u7684\u00a0\u6700\u957f\u5b50\u4e32\u00a0\u7684\u957f\u5ea6\u3002<\/p>\n<p>\u793a\u4f8b\u00a01:<\/p>\n<pre><code>\u8f93\u5165: s = &quot;abcabcbb&quot;\n\u8f93\u51fa: 3 \n\u89e3\u91ca: \u56e0\u4e3a\u65e0\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\u662f &quot;abc&quot;\uff0c\u6240\u4ee5\u5176\u957f\u5ea6\u4e3a 3\u3002<\/code><\/pre>\n<p>\u793a\u4f8b 2:<\/p>\n<pre><code>\u8f93\u5165: s = &quot;bbbbb&quot;\n\u8f93\u51fa: 1\n\u89e3\u91ca: \u56e0\u4e3a\u65e0\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\u662f &quot;b&quot;\uff0c\u6240\u4ee5\u5176\u957f\u5ea6\u4e3a 1\u3002<\/code><\/pre>\n<p>\u793a\u4f8b 3:<\/p>\n<pre><code>\u8f93\u5165: s = &quot;pwwkew&quot;\n\u8f93\u51fa: 3\n\u89e3\u91ca: \u56e0\u4e3a\u65e0\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\u662f\u00a0&quot;wke&quot;\uff0c\u6240\u4ee5\u5176\u957f\u5ea6\u4e3a 3\u3002\n\u00a0    \u8bf7\u6ce8\u610f\uff0c\u4f60\u7684\u7b54\u6848\u5fc5\u987b\u662f \u5b50\u4e32 \u7684\u957f\u5ea6\uff0c&quot;pwke&quot;\u00a0\u662f\u4e00\u4e2a\u5b50\u5e8f\u5217\uff0c\u4e0d\u662f\u5b50\u4e32\u3002<\/code><\/pre>\n<p>\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09<br \/>\n\u94fe\u63a5\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/longest-substring-without-repeating-characters\">https:\/\/leetcode.cn\/problems\/longest-substring-without-repeating-characters<\/a><br \/>\n\u8457\u4f5c\u6743\u5f52\u9886\u6263\u7f51\u7edc\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u5b98\u65b9\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E8%A7%A3%E9%A2%98\"><\/span>\u89e3\u9898<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3\"><\/span>\u6ed1\u52a8\u7a97\u53e3<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u5b9a\u4e49\u4e24\u4e2a\u6307\u9488 left \u548c right\uff0c\u5206\u522b\u6307\u5411\u67e5\u627e\u4e2d\u7684\u65e0\u91cd\u590d\u5b50\u4e32\u7684\u5de6\u53f3\u8fb9\u754c\uff0c\u521d\u59cb left \u6307\u5411\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff0cright \u6307\u5411\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u3002<br \/>\n\u5916\u5c42\u5faa\u73af\u6bcf\u6b21\u5c06\u53f3\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u6b21\uff0c\u5185\u5c42\u5faa\u73af\u8d1f\u8d23\u904d\u5386\u4ece left \u5230 right \u7684\u524d\u4e00\u4e2a\u5b57\u7b26\uff0c\u5c06\u5b83\u4eec\u6328\u4e2a\u548c right \u4f4d\u7f6e\u7684\u503c\u6bd4\u8f83\uff0c\u5982\u679c left \u5230 right \u524d\u4e00\u4e2a\u5b57\u7b26\u4e4b\u95f4\u5b58\u5728\u548c right \u4f4d\u7f6e\u76f8\u540c\u7684\u5b57\u7b26\uff0c\u5219\u5c06 left \u79fb\u52a8\u5230\u51fa\u73b0\u91cd\u590d\u5b57\u7b26\u7684\u4f4d\u7f6e\u7684\u4e0b\u4e00\u4e2a\u5b57\u7b26\u7684\u4f4d\u7f6e\uff0c\u5e76\u9000\u51fa\u5185\u5c42\u5faa\u73af\uff0c\u5916\u5faa\u73af\u4e5f\u4f1a\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff1b\u5982\u679c\u904d\u5386\u5b8c\u90fd\u6ca1\u627e\u5230\u548c right \u4f4d\u7f6e\u76f8\u540c\u7684\uff0c\u5219\u6b64\u8f6e\u5185\u5c42\u5faa\u73af\u5b8c\u6210\u904d\u5386\u540e\uff0c\u8ba1\u7b97 left~right \u4e4b\u95f4\u6709\u591a\u5c11\u4e2a\u5b57\u7b26\uff0c\u5e76\u548c\u5df2\u7ecf\u8bb0\u5f55\u7684\u6700\u5927\u503c\u6bd4\u8f83\uff0c\u6700\u7ec8\u8bb0\u5f55\u6700\u5927\u7684\u503c\u3002<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a <code class=\"katex-inline\">O(n^2)<\/code><\/p>\n<pre><code class=\"language-c\">int lengthOfLongestSubstring(char * s)\n{\n    int len = strlen(s);\n    if (len == 0)\n    {\n        return 0;\n    }\n\n    int max = 1;\n\n    for(int i, left = 0, right = 1; right &lt; len; ++right)\n    {\n        for(i = left; i &lt; right; ++i)\n        {\n            if(s[i] == s[right])\n            {\n                left = i + 1;\n                break;\n            }\n        }\n        if(i == right &amp;&amp; right - left + 1 &gt; max)\n        {\n            max =  right - left + 1;\n        }\n    }\n    return max;\n}<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2023\/03\/image-1680271772407.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 382px; --smush-placeholder-aspect-ratio: 382\/103;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3_%E5%93%88%E5%B8%8C%E8%A1%A8\"><\/span>\u6ed1\u52a8\u7a97\u53e3 + \u54c8\u5e0c\u8868<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u7b97\u662f\u4e0a\u4e00\u79cd\u65b9\u6cd5\u7684\u6539\u8fdb\uff0c\u4e3b\u8981\u53d8\u5316\u5c31\u662f\u5728\u67e5\u627e\u65b9\u6cd5\u4e0a\uff0c\u4e0a\u4e00\u4e2a\u662f\u7ebf\u6027\u67e5\u627e\uff0c\u8981\u6328\u4e2a\u904d\u5386\u5143\u7d20\uff0c\u8fd9\u91cc\u6362\u6210\u4e86\u54c8\u5e0c\u8868\uff0c\u5728\u67e5\u627e\u7b97\u6cd5\u4e0a\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4ece<code class=\"katex-inline\">O(n)<\/code>\u964d\u4e3a\u4e86<code class=\"katex-inline\">O(1)<\/code>,\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u5219\u53d8\u6210\u4e86<code class=\"katex-inline\">O(n)<\/code>\u3002\u8fd9\u91cc\u529b\u6263\u663e\u793a\u7684\u6267\u884c\u65f6\u95f4\u5dee\u4e0d\u591a\uff0c\u5e94\u8be5\u662f\u6570\u636e\u91cf\u5c0f\u7684\u539f\u56e0\uff0c\u4f53\u73b0\u4e0d\u51fa\u5dee\u8ddd\uff0c\u5982\u679c\u6570\u636e\u91cf\u975e\u5e38\u5927\u5c31\u4f1a\u5f88\u660e\u663e\u4e86\u3002<br \/>\n\u8fd9\u91cc\u7684\u5b57\u7b26\u4e32\u7ec4\u6210\u90fd\u662fASCII\u7801\uff0c\u6240\u4ee5\u5c31\u5b9a\u4e49\u4e86\u4e00\u4e2a128\u5927\u5c0f\u7684\u6570\u7ec4\uff08\u54c8\u5e0c\u8868\uff09\uff0c\u7528\u4e8e\u4fdd\u5b58\u6700\u8fd1\u51fa\u73b0\u67d0\u4e2a\u5b57\u7b26\u7684\u4f4d\u7f6e\uff08\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u4e0b\u6807\uff09\u3002\u5faa\u73af\u6bcf\u6b21\u5c06\u53f3\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u7136\u540e\u6bd4\u8f83 left \u548c\u6700\u8fd1\u4e00\u6b21\u91cd\u590d\u67d0\u4e2a\u5b57\u7b26\u7684\u4f4d\u7f6e\u7684\uff08\u4e0b\u4e00\u4f4d\u7684\uff09\u4e0b\u6807\uff0c\u6709\u91cd\u590d\u5c31\u5c06\u5de6\u6307\u9488\u79fb\u52a8\u5230\u91cd\u590d\u4f4d\u7f6e\u7684\u4e0b\u4e00\u4f4d\uff0c\u7136\u540e\u8ba1\u7b97\u5de6\u53f3\u6307\u9488\u4e4b\u95f4\u7684\u5b57\u7b26\u6570\u548c\u4e4b\u524d\u8bb0\u5f55\u7684\u6700\u5927\u503c\u6bd4\u8f83\uff0c\u8bb0\u5f55\u6700\u7ec8\u7684\u6700\u5927\u503c\uff0c\u7136\u540e\u53c8\u8bb0\u5f55\u5f53\u524dright\u4f4d\u7f6e\u7684\u5b57\u7b26\u7684\u4e0b\u6807\u7684\u4e0b\u4e00\u4f4d\u3002<\/p>\n<p>\u6ce8\uff1afmax \u662f\u6570\u5b66\u5e93\u4e2d\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u627e\u51fa\u4e24\u4e2a\u6570\u4e2d\u7684\u6700\u5927\u503c<\/p>\n<pre><code class=\"language-c\">int lengthOfLongestSubstring(char *s)\n{\n    int len = strlen(s);\n    int max = 0;\n    int index[128] = {0};\n\n    for (int left = 0, right = 0; right &lt; len; ++right)\n    {\n        left = fmax(left, index[s[right]]);\n        max = fmax(max, right - left + 1);\n        index[s[right]] = right + 1;\n    }\n    return max;\n}<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2023\/03\/image-1680273472424.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 385px; --smush-placeholder-aspect-ratio: 385\/130;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee \u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32 s \uff0c\u8bf7\u4f60\u627e\u51fa\u5176\u4e2d\u4e0d\u542b\u6709\u91cd\u590d\u5b57\u7b26\u7684\u00a0\u6700\u957f\u5b50\u4e32\u00a0\u7684\u957f\u5ea6\u3002 \u793a\u4f8b\u00a01: \u8f93\u5165: s = &#038;q [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,613],"tags":[],"class_list":["post-9953","post","type-post","status-publish","format-standard","hentry","category-all","category-613"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/9953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9953"}],"version-history":[{"count":0,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/9953\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}