{"id":70,"date":"2026-03-29T12:57:33","date_gmt":"2026-03-29T04:57:33","guid":{"rendered":"https:\/\/geocommunity.vip\/?p=70"},"modified":"2026-03-29T15:16:04","modified_gmt":"2026-03-29T07:16:04","slug":"%e6%bb%91%e5%8a%a8%e7%aa%97%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/geocommunity.vip\/index.php\/2026\/03\/29\/%e6%bb%91%e5%8a%a8%e7%aa%97%e5%8f%a3\/","title":{"rendered":"\u6ed1\u52a8\u7a97\u53e3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">1\u3001\u65e0\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff081\uff09\u9898\u76ee\u63cf\u8ff0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\u00a0<code>s<\/code>\u00a0\uff0c\u8bf7\u4f60\u627e\u51fa\u5176\u4e2d\u4e0d\u542b\u6709\u91cd\u590d\u5b57\u7b26\u7684\u00a0<strong>\u6700\u957f\u00a0\u5b50\u4e32\u00a0<\/strong>\u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff082\uff09\u89e3\u9898\u601d\u8def<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ed1\u52a8\u7a97\u53e3left right\uff0c\u6ed1\u52a8right\uff0c\u7528\u6570\u7ec4flag\u5b58\u50a8\u5b57\u7b26right\u662f\u5426\u5728\u7a97\u53e3\u5185\u51fa\u73b0\u8fc7\uff0c\u5176\u4e2dright\u4e3a\u7d22\u5f15\u3002\u5982\u679c\u51fa\u73b0\u8fc7\uff0c\u6ed1\u52a8left\uff0c\u76f4\u5230\u7b26\u5408\u60c5\u51b5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff083\uff09\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;string.h>\n#define MAX(a, b) ((a) > (b) ? a : b)\n\nint lengthOfLongestSubstring(char* s) {\n\n    int flag&#91;128];\n    for (int i = 0; i &lt; 128; i++) flag&#91;i] = 0;\n\n    int n = strlen(s);\n    int left = 0;\n    int right = 0;\n    int maxlen = 0;\n\n    while (right &lt; n &amp;&amp; left &lt;= right) {\n        \n        \/\/ \u7a97\u53e3\u5185\u51fa\u73b0\u8fc7\uff0c\u4e0d\u65ad\u66f4\u65b0left\uff0c\u76f4\u5230\u7b26\u5408\u60c5\u51b5\n        while (flag&#91;(int)s&#91;right]] == 1) {\n            flag&#91;(int)s&#91;left]] = 0;\n            left++;\n            continue;\n        }\n\n        flag&#91;(int)s&#91;right]] = 1;\n\n        maxlen = MAX(maxlen, right - left + 1);\n\n        right++;\n    }\n\n    return maxlen;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u7231\u751f\u6c14\u7684\u4e66\u5e97\u8001\u677f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff081\uff09\u9898\u76ee\u63cf\u8ff0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u4e00\u4e2a\u4e66\u5e97\u8001\u677f\uff0c\u4ed6\u7684\u4e66\u5e97\u5f00\u4e86 <code>n<\/code> \u5206\u949f\u3002\u6bcf\u5206\u949f\u90fd\u6709\u4e00\u4e9b\u987e\u5ba2\u8fdb\u5165\u8fd9\u5bb6\u5546\u5e97\u3002\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a <code>n<\/code> \u7684\u6574\u6570\u6570\u7ec4 <code>customers<\/code>\uff0c\u5176\u4e2d <code>customers[i]<\/code> \u662f\u5728\u7b2c <code>i<\/code> \u5206\u949f\u5f00\u59cb\u65f6\u8fdb\u5165\u5546\u5e97\u7684\u987e\u5ba2\u6570\u91cf\uff0c\u6240\u6709\u8fd9\u4e9b\u987e\u5ba2\u5728\u7b2c <code>i<\/code> \u5206\u949f\u7ed3\u675f\u540e\u79bb\u5f00\u3002\u5728\u67d0\u4e9b\u5206\u949f\u5185\uff0c\u4e66\u5e97\u8001\u677f\u4f1a\u751f\u6c14\u3002\u5982\u679c\u4e66\u5e97\u8001\u677f\u5728\u7b2c <code>i<\/code> \u5206\u949f\u751f\u6c14\uff0c\u90a3\u4e48 <code>grumpy[i] = 1<\/code>\uff0c\u5426\u5219 <code>grumpy[i] = 0<\/code>\u3002\u5f53\u4e66\u5e97\u8001\u677f\u751f\u6c14\u65f6\uff0c\u90a3\u4e00\u5206\u949f\u7684\u987e\u5ba2\u5c31\u4f1a\u4e0d\u6ee1\u610f\uff0c\u82e5\u8001\u677f\u4e0d\u751f\u6c14\u5219\u987e\u5ba2\u662f\u6ee1\u610f\u7684\u3002\u4e66\u5e97\u8001\u677f\u77e5\u9053\u4e00\u4e2a\u79d8\u5bc6\u6280\u5de7\uff0c\u80fd\u6291\u5236\u81ea\u5df1\u7684\u60c5\u7eea\uff0c\u53ef\u4ee5\u8ba9\u81ea\u5df1\u8fde\u7eed <code>minutes<\/code> \u5206\u949f\u4e0d\u751f\u6c14\uff0c\u4f46\u5374\u53ea\u80fd\u4f7f\u7528\u4e00\u6b21\u3002\u8bf7\u4f60\u8fd4\u56de\u8fd9\u4e00\u5929\u8425\u4e1a\u4e0b\u6765\uff0c\u6700\u591a\u6709\u591a\u5c11\u5ba2\u6237\u80fd\u591f\u611f\u5230\u6ee1\u610f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff082\uff09\u89e3\u9898\u601d\u8def<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b54\u6848\u5206\u4e3a\u56fa\u5b9a\u6536\u76ca\u548c\u989d\u5916\u6536\u76ca\u4e24\u90e8\u5206\uff0c\u56fa\u5b9a\u6536\u76ca\u5f88\u7b80\u5355\u3002\u5bf9\u4e8e\u989d\u5916\u6536\u76ca\uff0c\u6211\u4eec\u5148\u521d\u59cb\u5316\u4e00\u4e2a\u7a97\u53e3\uff0c\u7136\u540e\u4e0d\u65ad\u6ed1\u52a8\uff0c\u904d\u5386\u6240\u6709\u7a97\u53e3\uff0c\u5f97\u5230\u6240\u6709\u7a97\u53e3\u7684\u989d\u5916\u6536\u76ca\u6700\u5927\u503c\u5373\u53ef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff083\uff09\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define MAX(a, b) ((a) > (b) ? a : b)\n\nint maxSatisfied(int* customers, int customersSize, int* grumpy, int grumpySize, int minutes) {\n    \/\/ \u8ba1\u7b97\u56fa\u5b9a\u6536\u76ca\n    int fixed = 0;\n\n    for (int i = 0; i &lt; grumpySize; i++) {\n        if (grumpy&#91;i] == 0) fixed += customers&#91;i];\n    }\n\n    \/\/ \u4f46\u662f\u76ee\u524d\u591a\u4e86\u4e00\u4e2a\u53c2\u6570minutes \u90a3\u4e48\u6211\u4eec\u5229\u7528\u6ed1\u52a8\u7a97\u53e3\u8ba1\u7b97\u989d\u5916\u7684\u6536\u76caextra_max\n    int c_extra = 0;\n    for (int i = 0; i &lt; minutes; i++) {\n        if (grumpy&#91;i] == 1) c_extra += customers&#91;i];\n    }\n\n    \/\/ \u6ed1\u52a8\u7a97\u53e3\n    int extra_max = c_extra;\n    for (int i = minutes; i &lt; grumpySize; i++) {\n        if (grumpy&#91;i] == 1) c_extra += customers&#91;i]; \/\/ \u63a8\u8fdb\n        if (grumpy&#91;i-minutes] == 1) c_extra -= customers&#91;i-minutes]; \/\/ \u6536\u7f29\n\n        extra_max = MAX(extra_max, c_extra);\n    }\n\n    return fixed + extra_max;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u53cc\u6307\u9488\u6ed1\u52a8\u7a97\u53e3<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff081\uff09\u9898\u76ee\u63cf\u8ff0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6570\u7ec4 <code>nums<\/code> \u548c\u4e00\u4e2a\u6574\u6570 <code>k<\/code>\uff0c\u5047\u8bbe\u6700\u591a\u53ef\u4ee5\u7ffb\u8f6c <code>k<\/code> \u4e2a <code>0<\/code>\uff0c\u5219\u8fd4\u56de\u6267\u884c\u64cd\u4f5c\u540e<strong>\u6570\u7ec4\u4e2d\u8fde\u7eed 1 \u7684\u6700\u5927\u4e2a\u6570<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff082\uff09\u89e3\u9898\u601d\u8def<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53cc\u6307\u9488 <code>left<\/code>\u3001<code>right<\/code>\uff0c<code>right<\/code> \u4e0d\u65ad\u5411\u540e\u79fb\u52a8\uff0c\u76f4\u5230 <code>k<\/code> \u4e3a 0 \u65f6\u624d\u5f00\u59cb\u79fb\u52a8 <code>left<\/code>\uff0c\u671f\u95f4\u4e0d\u65ad\u66f4\u65b0\u7b54\u6848\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff083\uff09\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int longestOnes(int* nums, int numsSize, int k) {\n\n    \/\/ \u521d\u59cb\u5316\n    int left = 0, right = 0;\n    int ans = 0;\n\n    \/\/ \u6ed1\u52a8\u7a97\u53e3\n    while (right &lt; numsSize) {\n        if (nums&#91;right] == 0) k--;\n\n        while (k &lt; 0) {\n            if (nums&#91;left] == 0) k++;\n            left++;\n        }\n\n        int c_ans = right - left + 1;\n        if (c_ans > ans) ans = c_ans;\n\n        right++;\n    }\n\n    return ans;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u65e0\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32 \uff081\uff09\u9898\u76ee\u63cf\u8ff0 \u7ed9\u5b9a\u4e00\u4e2a &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":3,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":76,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/70\/revisions\/76"}],"wp:attachment":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}