{"id":96,"date":"2026-04-05T18:08:44","date_gmt":"2026-04-05T10:08:44","guid":{"rendered":"https:\/\/geocommunity.vip\/?p=96"},"modified":"2026-04-05T18:08:44","modified_gmt":"2026-04-05T10:08:44","slug":"%e5%8a%9b%e6%89%a3hot100%e4%b8%ad%e7%9a%84%e5%9b%b0%e9%9a%be%e9%a2%98","status":"publish","type":"post","link":"https:\/\/geocommunity.vip\/index.php\/2026\/04\/05\/%e5%8a%9b%e6%89%a3hot100%e4%b8%ad%e7%9a%84%e5%9b%b0%e9%9a%be%e9%a2%98\/","title":{"rendered":"\u529b\u6263Hot100\u4e2d\u7684\u56f0\u96be\u9898"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1\u3001<a href=\"https:\/\/leetcode.cn\/problems\/longest-valid-parentheses\/\">\u6700\u957f\u6709\u6548\u62ec\u53f7<\/a>\uff08\u6808\uff09<\/h3>\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\u4f60\u4e00\u4e2a\u53ea\u5305\u542b&nbsp;<code>'('<\/code>&nbsp;\u548c&nbsp;<code>')'<\/code>&nbsp;\u7684\u5b57\u7b26\u4e32\uff0c\u627e\u51fa\u6700\u957f\u6709\u6548\uff08\u683c\u5f0f\u6b63\u786e\u4e14\u8fde\u7eed\uff09\u62ec\u53f7&nbsp;\u5b50\u4e32&nbsp;\u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5de6\u53f3\u62ec\u53f7\u5339\u914d\uff0c\u5373\u6bcf\u4e2a\u5de6\u62ec\u53f7\u90fd\u6709\u5bf9\u5e94\u7684\u53f3\u62ec\u53f7\u5c06\u5176\u95ed\u5408\u7684\u5b57\u7b26\u4e32\u662f\u683c\u5f0f\u6b63\u786e\u7684\uff0c\u6bd4\u5982&nbsp;<code>\"(()())\"<\/code>\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\">\u9047\u5230\u201c\uff08\u201d\u5165\u6808\uff0c\u9047\u5230\u201c\uff09\u201d\u51fa\u6808\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff083\uff09\u9519\u8bef\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;string.h>\n#define MAX(a, b) ((a) > (b) ? a : b)\nint longestValidParentheses(char* s) {\n    <strong>\/\/ ()(()\u4f1a\u8f93\u51fa4 \u4f46\u662f\u7b54\u6848\u4e3a2<\/strong>\n    \/\/ \u9047\u5230(\u538b\u5165\u6808\u4e2d\uff0c\u9047\u5230)\u8fdb\u884c\u51fa\u6808\uff0c\u5982\u679c\u51fa\u6808\u5931\u8d25c_ans\u91cd\u7f6e\u4e3a0\uff0c\u5426\u5219c_ans++\uff0c\u540c\u65f6\u4e0d\u65ad\u66f4\u65b0ans\n    int ans = 0;\n    int n = strlen(s); \n    char z&#91;n];\n    int bottom = -1; \/\/ \u5b9a\u4e49\u6808\u5c3e\u6307\u9488\n    int c_ans = 0;\n    for (int i = 0; i &lt; n; i++) {\n        if (bottom == -1 &amp;&amp; s&#91;i] == \")\") continue;\n        if (s&#91;i] == '(') {\n            z&#91;++bottom] = '('; \/\/ \u5165\u6808\n        } else {\n            if (z&#91;bottom] == '(') { \/\/ \u51fa\u6808\n                c_ans += 2;\n                bottom--;\n                ans = MAX(ans, c_ans);\n            } else {\n                c_ans = 0;\n            }\n        }\n    }\n    return ans;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\uff084\uff09\u6b63\u786e\u89e3\u6cd5\uff0c\u6808\u4e2d\u5b58\u50a8\u4e0b\u6807<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;string.h>\n#define MAX(a, b) ((a) > (b) ? a : b)\nint longestValidParentheses(char* s) {\n    int n = strlen(s);\n    int ans = 0;\n\n    int stack&#91;n+1];\n    int top = -1;\n    <strong>stack&#91;++top] = -1; \/\/ \u521d\u59cb\u53c2\u8003<\/strong>\n    for (int i = 0; i &lt; n; i++) {\n        if (s&#91;i] == '(') {\n            stack&#91;++top] = i; \/\/ \u5165\u6808\n        } else {\n            --top; \/\/ \u5148\u51fa\u6808\n            if (top == -1) { \/\/ \u8bf4\u660e\u201c\uff09\u201d\u76ee\u524d\u591a\u4f59\n                <strong>stack&#91;++top] = i; \/\/ \u65b0\u53c2\u8003<\/strong>\n            } else {\n                <strong>ans = MAX(ans, i - stack&#91;top]);<\/strong>\n            }\n        }\n    }\n    return ans;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2\u3001<a href=\"https:\/\/leetcode.cn\/problems\/largest-rectangle-in-histogram\/\">\u67f1\u72b6\u56fe\u4e2d\u6700\u5927\u7684\u77e9\u5f62<\/a>\uff08\u6808\uff09<\/h3>\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&nbsp;<em>n<\/em>&nbsp;\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u7528\u6765\u8868\u793a\u67f1\u72b6\u56fe\u4e2d\u5404\u4e2a\u67f1\u5b50\u7684\u9ad8\u5ea6\u3002\u6bcf\u4e2a\u67f1\u5b50\u5f7c\u6b64\u76f8\u90bb\uff0c\u4e14\u5bbd\u5ea6\u4e3a 1 \u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6c42\u5728\u8be5\u67f1\u72b6\u56fe\u4e2d\uff0c\u80fd\u591f\u52fe\u52d2\u51fa\u6765\u7684\u77e9\u5f62\u7684\u6700\u5927\u9762\u79ef\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\">\u00a0 \u00a0 \u7b2c\u4e00\u79cd\u89e3\u6cd5\u662f\u66b4\u529b\uff0c\u679a\u4e3e\u6240\u6709left\u548cright\uff0c\u7136\u540e\u4e0d\u65ad\u66f4\u65b0\u7b54\u6848.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">    \u7b2c\u4e8c\u79cd\u89e3\u6cd5\u662f\u7ef4\u62a4\u4e00\u4e2a\u5355\u8c03\u9012\u589e\u7684\u6808\u3002\u7a77\u4e3e\uff1a\u8ba1\u7b97\u4e86\u4ee5\u6240\u6709\u67f1\u5b50\u9ad8\u5ea6\u4e3a\u77e9\u5f62\u9ad8\u5ea6\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; \u5982\u679c\u9047\u5230\u4e00\u4e2a\u9ad8\u5ea6a\u6bd4\u6808\u9876\u7d22\u5f15\u5bf9\u5e94\u7684\u9ad8\u5ea6b\u5c0f\uff0c\u5373\uff0c\u5f39\u51fa\u6808\u9876\uff0c\u5e76\u628aa\u7684\u7d22\u5f15\u4f5c\u4e3a\u53f3\u8fb9\u754cj\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u00a0 \u00a0 \u628a\u5f53\u524d\u6808\u9876\u4f5c\u4e3a\u5de6\u8fb9\u754ci\u3002 j-i-1\u5c31\u662f\u5bbd\u5ea6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; \u6808\u662f\u66f4\u597d\u7684\u53d1\u73b0\u5de6\u8fb9\u754c\u548c\u53f3\u8fb9\u754c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff083\uff09\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define MAX(a, b) ((a) > (b) ? (a) : (b))\n\nint largestRectangleArea(int* heights, int heightsSize) {\n    int* stack = (int*)malloc(sizeof(int) * <strong>(heightsSize + 2)<\/strong>);\n    int top = -1;\n    int max_area = 0;\n\n\n    for (int i = -1; i &lt;= heightsSize; i++) {\n        \/\/ \u5de6\u53f30\n        <strong>int cur_h = (i == -1 || i == heightsSize) ? 0 : heights&#91;i];<\/strong>\n\n        while (top != -1 &amp;&amp; <strong>((stack&#91;top] == -1) ? 0 : heights&#91;stack&#91;top]]) > cur_h<\/strong>) {\n            int h = heights&#91;stack&#91;top--]];\n            \n            \/\/ <strong>\u6b64\u65f6\u6808\u9876\u5c31\u662f\u5de6\u8fb9\u754c\uff0c\u5f53\u524d i \u5c31\u662f\u53f3\u8fb9\u754c<\/strong>\n            int left_idx = stack&#91;top];\n            int width = i - left_idx - 1;\n            \n            max_area = MAX(max_area, h * width);\n        }\n        stack&#91;++top] = i;\n    }\n\n    free(stack);\n    return max_area;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3\u3001<a href=\"https:\/\/leetcode.cn\/problems\/median-of-two-sorted-arrays\/\">\u5bfb\u627e\u4e24\u4e2a\u6b63\u5e8f\u6570\u7ec4\u7684\u4e2d\u4f4d\u6570<\/a>\uff08\u4e8c\u5206\uff09<\/h3>\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\u4e24\u4e2a\u5927\u5c0f\u5206\u522b\u4e3a&nbsp;<code>m<\/code>&nbsp;\u548c&nbsp;<code>n<\/code>&nbsp;\u7684\u6b63\u5e8f\uff08\u4ece\u5c0f\u5230\u5927\uff09\u6570\u7ec4&nbsp;<code>nums1<\/code>&nbsp;\u548c&nbsp;<code>nums2<\/code>\u3002\u8bf7\u4f60\u627e\u51fa\u5e76\u8fd4\u56de\u8fd9\u4e24\u4e2a\u6b63\u5e8f\u6570\u7ec4\u7684&nbsp;<strong>\u4e2d\u4f4d\u6570<\/strong>&nbsp;\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5e94\u8be5\u4e3a&nbsp;<code>O(log (m+n))<\/code>&nbsp;\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\">\u5148\u5408\u5e76\u6570\u7ec4\uff0c\u518d\u4e8c\u5206\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff083\uff09\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n#include &lt;stdlib.h>\n\ndouble findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {\n    int totalSize = nums1Size + nums2Size;\n    <strong>int* merged = (int*)malloc(sizeof(int) * totalSize); \/\/ \u5f00\u8f9f\u5185\u5b58<\/strong>\n    \n    \/\/ \u5408\u5e76\u4e24\u4e2a\u6570\u7ec4\n    int i = 0, j = 0, k = 0;\n    while (i &lt; nums1Size &amp;&amp; j &lt; nums2Size) {\n        if (nums1&#91;i] &lt; nums2&#91;j]) {\n            merged&#91;k++] = nums1&#91;i++];\n        } else {\n            merged&#91;k++] = nums2&#91;j++];\n        }\n    }\n    \n    \/\/ \u5904\u7406\u5269\u4f59\u7684\u5143\u7d20\n    if (nums1Size > nums2Size) while (i &lt; nums1Size) merged&#91;k++] = nums1&#91;i++];\n    else while (j &lt; nums2Size) merged&#91;k++] = nums2&#91;j++];\n\n    \/\/ \u8ba1\u7b97\u4e2d\u4f4d\u6570\n    if (totalSize % 2 == 1) return merged&#91;totalSize \/ 2];\n    else return (merged&#91;totalSize \/ 2 - 1] + merged&#91;totalSize \/ 2]) \/ <strong>2.0<\/strong>;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4\u3001<a href=\"https:\/\/leetcode.cn\/problems\/n-queens\/\">N \u7687\u540e<\/a>\uff08DFS\uff09<\/h3>\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\">\u6309\u7167\u56fd\u9645\u8c61\u68cb\u7684\u89c4\u5219\uff0c\u7687\u540e\u53ef\u4ee5\u653b\u51fb\u4e0e\u4e4b\u5904\u5728\u540c\u4e00\u884c\u6216\u540c\u4e00\u5217\u6216\u540c\u4e00\u659c\u7ebf\u4e0a\u7684\u68cb\u5b50\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>n&nbsp;\u7687\u540e\u95ee\u9898<\/strong>&nbsp;\u7814\u7a76\u7684\u662f\u5982\u4f55\u5c06&nbsp;<code>n<\/code>&nbsp;\u4e2a\u7687\u540e\u653e\u7f6e\u5728&nbsp;<code>n\u00d7n<\/code>&nbsp;\u7684\u68cb\u76d8\u4e0a\uff0c\u5e76\u4e14\u4f7f\u7687\u540e\u5f7c\u6b64\u4e4b\u95f4\u4e0d\u80fd\u76f8\u4e92\u653b\u51fb\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570&nbsp;<code>n<\/code>&nbsp;\uff0c\u8fd4\u56de\u6240\u6709\u4e0d\u540c\u7684&nbsp;<strong>n<em>&nbsp;<\/em>\u7687\u540e\u95ee\u9898<\/strong>&nbsp;\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bcf\u4e00\u79cd\u89e3\u6cd5\u5305\u542b\u4e00\u4e2a\u4e0d\u540c\u7684&nbsp;<strong>n \u7687\u540e\u95ee\u9898<\/strong>&nbsp;\u7684\u68cb\u5b50\u653e\u7f6e\u65b9\u6848\uff0c\u8be5\u65b9\u6848\u4e2d&nbsp;<code>'Q'<\/code>&nbsp;\u548c&nbsp;<code>'.'<\/code>&nbsp;\u5206\u522b\u4ee3\u8868\u4e86\u7687\u540e\u548c\u7a7a\u4f4d\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\">DFS+\u56de\u6eaf<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff083\uff09\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;string.h>\n#include &lt;stdbool.h>\n\n<em><strong>\/\/ \u68c0\u67e5\u5728(row, col)\u653e\u7f6e\u7687\u540e\u662f\u5426\u5b89\u5168<\/strong><\/em>\nbool isSafe(int row, int col, int n, char** board) {\n    \/\/ 1.\u68c0\u67e5\u6b63\u4e0a\u65b9\n    for (int i = 0; i &lt; row; i++) {\n        if (board&#91;i]&#91;col] == 'Q') return false;\n    }\n    \/\/ 2.\u68c0\u67e5\u5de6\u4e0a\u65b9\u5bf9\u89d2\u7ebf\n    for (int i = row - 1, j = col - 1; i >= 0 &amp;&amp; j >= 0; i--, j--) {\n        if (board&#91;i]&#91;j] == 'Q') return false;\n    }\n    \/\/ 3.\u68c0\u67e5\u53f3\u4e0a\u65b9\u5bf9\u89d2\u7ebf\n    for (int i = row - 1, j = col + 1; i >= 0 &amp;&amp; j &lt; n; i--, j++) {\n        if (board&#91;i]&#91;j] == 'Q') return false;\n    }\n    return true;\n}\n\n<em><strong>\/\/ \u9012\u5f52\u51fd\u6570<\/strong><\/em>\nvoid backtrack(int row, int n, int* returnSize, char*** res, char** board) {\n    <strong>if (row == n) { \/\/ \u9012\u5f52\u51fa\u53e3\uff0c\u5b58\u50a8\u7b54\u6848<\/strong>\n        res&#91;*returnSize] = (char**)malloc(n * sizeof(char*));\n        for (int i = 0; i &lt; n; i++) {\n            res&#91;*returnSize]&#91;i] = (char*)malloc((n+1)*sizeof(char));\n            strcpy(res&#91;*returnSize]&#91;i], board&#91;i]);\n        }\n        (*returnSize)++;\n        return;\n    }\n    for (int col = 0; col &lt; n; col++) {\n        if (isSafe(row, col, n, board)) {\n            board&#91;row]&#91;col] = 'Q';\n            backtrack(row + 1, n, returnSize, res, board);\n            board&#91;row]&#91;col] = '.'; \/\/ for+\u56de\u6eaf\u679a\u4e3e\u6240\u6709\u60c5\u51b5\n        }\n    }\n}\n\n<em><strong>\/\/ \u4e3b\u51fd\u6570<\/strong><\/em>\nchar*** solveNQueens(int n, int* returnSize, int** returnColumnSizes) {\n    \/\/ 1.\u51c6\u5907\u7ed3\u679c\u6570\u7ec4\n    char*** res = (char***)malloc(1000 * sizeof(char**));\n    *returnSize = 0;\n    \/\/ 2.\u521d\u59cb\u5316\u7a7a\u7684\u68cb\u76d8\n    char** board = (char**)malloc(n * sizeof(char*));\n    for (int i = 0; i &lt; n; i++) {\n        board&#91;i] = (char*)malloc((n + 1) * sizeof(char));\n        for (int j = 0; j &lt; n; j++) board&#91;i]&#91;j] = '.';\n        board&#91;i]&#91;n] = '\\0';\n    }\n    \/\/ 3.\u5f00\u59cb\u9012\u5f52\n    backtrack(0, n, returnSize, res, board);\n\n    \/\/ 4.\u8bb0\u5f55\u6bcf\u4e2a\u89e3\u7684\u884c\u6570\n    <strong>*returnColumnSizes = (int*)malloc((*returnSize) * sizeof(int));<\/strong>\n    for (int i =0; i &lt; *returnSize; i++) {\n        (*returnColumnSizes)&#91;i] = n;\n    }\n\n    return res;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5\u3001<a href=\"https:\/\/leetcode.cn\/problems\/minimum-window-substring\/\">\u6700\u5c0f\u8986\u76d6\u5b50\u4e32<\/a>\uff08\u53cc\u6307\u9488\uff09<\/h3>\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\u4e24\u4e2a\u5b57\u7b26\u4e32\u00a0<code>s<\/code>\u00a0\u548c\u00a0<code>t<\/code>\uff0c\u957f\u5ea6\u5206\u522b\u662f\u00a0<code>m<\/code>\u00a0\u548c\u00a0<code>n<\/code>\uff0c\u8fd4\u56de s \u4e2d\u7684\u00a0<strong>\u6700\u77ed\u7a97\u53e3\u00a0\u5b50\u4e32<\/strong>\uff0c\u4f7f\u5f97\u8be5\u5b50\u4e32\u5305\u542b\u00a0<code>t<\/code>\u00a0\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff08<strong>\u5305\u62ec\u91cd\u590d\u5b57\u7b26<\/strong>\uff09\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u6837\u7684 \u5b50\u4e32\uff0c\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32<em>\u00a0<\/em><code>\"\"<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6d4b\u8bd5\u7528\u4f8b\u4fdd\u8bc1\u7b54\u6848\u552f\u4e00\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\u53e3<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff083\uff09\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;string.h>\nchar* minWindow(char* s, char* t) {\n\n    int m = strlen(s), n = strlen(t);\n    int reference&#91;128] = {0};\n    for (int i = 0; i &lt; n; i++) reference&#91;t&#91;i]]++;\n    int window&#91;128] = {0}; \/\/ \u8bb0\u5f55\u5f53\u524d\u7a97\u53e3\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\n    int total_kinds = 0;\n    int current_kinds = 0;\n    for (int i = 0; i &lt; 128; i++) if (reference&#91;i] > 0) total_kinds++;\n    int left = 0, right = 0;\n    int ans_len = m + 1; \/\/ \u8bbe\u7f6e\u4e00\u4e2a\u4e0d\u53ef\u80fd\u7684\u7b54\u6848\n    <strong>int start = 0;<\/strong>\n\n    while (right &lt; m) {\n        if (reference&#91;s&#91;right]] > 0) {\n            window&#91;s&#91;right]]++;\n            if (window&#91;s&#91;right]] == reference&#91;s&#91;right]]) {\n                current_kinds++;\n            }\n        }\n        while (current_kinds == total_kinds) {\n            if ((right - left + 1) &lt;= ans_len) {\n<strong>               start = left; \/\/ \u66f4\u65b0\u8d77\u70b9\u4f4d\u7f6e\n               ans_len = right - left + 1; \/\/ \u66f4\u65b0\u957f\u5ea6<\/strong>\n            }\n            <strong>\/\/ \u6536\u7f29\u7a97\u53e3<\/strong>\uff08\u5f88\u6807\u51c6\u7684\u6536\u7f29\uff0c\u53ea\u6709\u5f53\u6ee1\u8db3\u60c5\u51b5\uff0c\u624d\u8fdb\u884c\u6ed1\u52a8\u5de6\u8fb9\u754c\u8fdb\u4e00\u6b65\u5224\u65ad\uff09\n            if (reference&#91;s&#91;left]] > 0) {\n                window&#91;s&#91;left]]--;\n                if (window&#91;s&#91;left]] &lt; reference&#91;s&#91;left]]) current_kinds--;\n            }\n            <strong>left++;<\/strong>\n        }\n        <strong>right++;<\/strong>\n    }\n\n    if (ans_len == m + 1) return \"\";\n    char* res = (char*)malloc((ans_len+1) * sizeof(char));\n    <strong>strncpy(res, s + start, ans_len + 1);<\/strong> \/\/+1\u8868\u793a\u8fde'\\0'\u4e00\u540c\u590d\u5236\u8fc7\u53bb\n    return res;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6\u3001<a href=\"https:\/\/leetcode.cn\/problems\/trapping-rain-water\/\">\u63a5\u96e8\u6c34<\/a>\uff08\u52a8\u6001\u89c4\u5212\u3001\u53cc\u6307\u9488\uff09<\/h3>\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\u00a0<code>n<\/code>\u00a0\u4e2a\u975e\u8d1f\u6574\u6570\u8868\u793a\u6bcf\u4e2a\u5bbd\u5ea6\u4e3a\u00a0<code>1<\/code>\u00a0\u7684\u67f1\u5b50\u7684\u9ad8\u5ea6\u56fe\uff0c\u8ba1\u7b97\u6309\u6b64\u6392\u5217\u7684\u67f1\u5b50\uff0c\u4e0b\u96e8\u4e4b\u540e\u80fd\u63a5\u591a\u5c11\u96e8\u6c34\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\">\u6728\u6876\u539f\u7406<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff083\uff09\u89e3\u9898<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\uff09\u52a8\u6001\u89c4\u5212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define MIN(a, b) ((a) &lt; (b) ? a : b)\n#define MAX(a, b) ((a) > (b) ? a : b)\nint trap(int* height, int heightSize) {\n<strong>    \/\/ \u5b9a\u4e49\u6570\u7ec4left_max_height\uff0cleft_max_height&#91;i]\u8868\u793a\u4f4d\u7f6ei\u5de6\u8fb9\u67f1\u5b50\u7684\u6700\u9ad8\u9ad8\u5ea6\u3002\n    \/\/ \u5b9a\u4e49\u6570\u7ec4right_max_height\uff0cright_max_height&#91;i]\u8868\u793a\u4f4d\u7f6ei\u53f3\u8fb9\u67f1\u5b50\u7684\u6700\u9ad8\u9ad8\u5ea6\u3002<\/strong>\n    int left_max_height&#91;heightSize];\n    int right_max_height&#91;heightSize];\n    for (int i = 0; i &lt; heightSize; i++) {\n        left_max_height&#91;i] = -1;\n        right_max_height&#91;i] = -1;\n    }\n\n    \/\/ \u52a8\u6001\u89c4\u5212\n    for (int i = 1; i &lt; heightSize-1; i++) {\n        left_max_height&#91;i] = MAX(height&#91;i-1], left_max_height&#91;i-1]);\n    }\n    for (int i = heightSize-2; i > 0; i--) {\n        right_max_height&#91;i] = MAX(height&#91;i+1], right_max_height&#91;i+1]);\n    }\n\n    int ans = 0;\n    for (int i = 1; i &lt; heightSize - 1; i++) {\n        if (MIN(left_max_height&#91;i], right_max_height&#91;i]) > height&#91;i]) {\n            ans += MIN(left_max_height&#91;i], right_max_height&#91;i]) - height&#91;i];\n        }\n    }\n\n    return ans;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2\uff09\u53cc\u6307\u9488<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int trap(int* height, int heightSize) {\n\n    int left = 0, right = heightSize - 1;\n    int l_max = 0, r_max = 0;\n    int ans = 0;\n\n    while (left &lt; right) {\n        \/\/ \u66f4\u65b0\u5de6\u53f3\u4e24\u8fb9\u7684\u6700\u9ad8\u7eaa\u5f55\n        if (height&#91;left] > l_max) l_max = height&#91;left];\n        if (height&#91;right] > r_max) r_max = height&#91;right];\n\n        <strong>\/\/ \u6838\u5fc3\u903b\u8f91\uff1a\u54ea\u8fb9\u77ee\uff0c\u5c31\u8ba1\u7b97\u54ea\u8fb9\u5e76\u79fb\u52a8\u54ea\u8fb9<\/strong>\n        if (l_max &lt; r_max) {\n            \/\/ \u5de6\u8fb9\u662f\u77ed\u677f\uff0c\u6c34\u91cf\u7531 l_max \u51b3\u5b9a\n            ans += l_max - height&#91;left];\n            left++;\n        } else {\n            \/\/ \u53f3\u8fb9\u662f\u77ed\u677f\uff0c\u6c34\u91cf\u7531 r_max \u51b3\u5b9a\n            ans += r_max - height&#91;right];\n            right--;\n        }\n    }\n\n    return ans;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u6700\u957f\u6709\u6548\u62ec\u53f7\uff08\u6808\uff09 \uff081\uff09\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e00\u4e2a\u53ea &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-96","post","type-post","status-publish","format-standard","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/96","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=96"}],"version-history":[{"count":1,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":97,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/96\/revisions\/97"}],"wp:attachment":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}