{"id":68107,"date":"2026-01-29T18:17:20","date_gmt":"2026-01-29T10:17:20","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/68107.html"},"modified":"2026-01-29T18:17:20","modified_gmt":"2026-01-29T10:17:20","slug":"%e3%80%90%e5%8a%9b%e6%89%a3problem238-%e9%99%a4%e4%ba%86%e8%87%aa%e8%ba%ab%e4%bb%a5%e5%a4%96%e6%95%b0%e7%bb%84%e7%9a%84%e4%b9%98%e7%a7%af%e3%80%91%e5%89%8d%e5%90%8e%e7%bc%80%e4%b9%98%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/68107.html","title":{"rendered":"\u3010\u529b\u6263Problem:238. \u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef\u3011\u524d\u540e\u7f00\u4e58\u6cd5 + \u53cc\u6307\u9488\u4f18\u5316"},"content":{"rendered":"<p>Problem:238. \u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef<\/p>\n<h2>\u9898\u76ee\u63cf\u8ff0<\/h2>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums&#xff0c;\u8fd4\u56de \u6570\u7ec4 answer &#xff0c;\u5176\u4e2d answer[i] \u7b49\u4e8e nums \u4e2d\u9664\u4e86 nums[i] \u4e4b\u5916\u5176\u4f59\u5404\u5143\u7d20\u7684\u4e58\u79ef \u3002<\/p>\n<p>\u9898\u76ee\u6570\u636e \u4fdd\u8bc1 \u6570\u7ec4 nums\u4e4b\u4e2d\u4efb\u610f\u5143\u7d20\u7684\u5168\u90e8\u524d\u7f00\u5143\u7d20\u548c\u540e\u7f00\u7684\u4e58\u79ef\u90fd\u5728 32 \u4f4d \u6574\u6570\u8303\u56f4\u5185\u3002<\/p>\n<p>\u8bf7 \u4e0d\u8981\u4f7f\u7528\u9664\u6cd5&#xff0c;\u4e14\u5728 O(n) \u65f6\u95f4\u590d\u6742\u5ea6\u5185\u5b8c\u6210\u6b64\u9898\u3002<\/p>\n<h3>\u793a\u4f8b1&#xff1a;<\/h3>\n<p>\u8f93\u5165&#xff1a; nums &#061; [1,2,3,4] \u8f93\u51fa&#xff1a; [24,12,8,6]<\/p>\n<h3>\u793a\u4f8b2&#xff1a;<\/h3>\n<p>\u8f93\u5165&#xff1a; nums &#061; [-1,1,0,-3,3] \u8f93\u51fa&#xff1a; [0,0,9,0,0]<\/p>\n<h3>\u63d0\u793a&#xff1a;<\/h3>\n<ul>\n<li>2 &lt;&#061; nums.length &lt;&#061; 10^5<\/li>\n<li>-30 &lt;&#061; nums[i] &lt;&#061; 30<\/li>\n<li>\u8f93\u5165 \u4fdd\u8bc1 \u6570\u7ec4 answer[i] \u5728 32 \u4f4d \u6574\u6570\u8303\u56f4\u5185<\/li>\n<\/ul>\n<h2>\u89e3\u6cd5\u4e00&#xff1a;\u524d\u540e\u7f00\u4e58\u6cd5<\/h2>\n<h4>\u6838\u5fc3\u601d\u8def<\/h4>\n<p>\u9898\u76ee\u8981\u6c42nums \u4e2d\u9664\u4e86 nums[i] \u4e4b\u5916\u5176\u4f59\u5404\u5143\u7d20\u7684\u4e58\u79ef &#xff0c;\u5c31\u662f\u6c42 nums[i] \u5de6\u8fb9\u5143\u7d20\u4e58\u79ef\u4e0e\u53f3\u8fb9\u5143\u7d20\u4e58\u79ef\u7684\u4e58\u79ef\u3002 \u53d7\u5230\u524d\u7f00\u548c\u7684\u542f\u53d1&#xff0c;\u6211\u4eec\u53ef\u4ee5\u6c42 nums \u6570\u7ec4\u7684\u524d\u540e\u7f00\u4e58\u79ef\u6570\u7ec4&#xff0c;\u5206\u522b\u8bb0\u4e3a ret_l \u548c ret_r&#xff1a;<\/p>\n<li>\u521d\u59cb\u5316&#xff1a;ret_l[0] &#061; 1&#xff0c;ret_r[n-1] &#061; 1 \u3002<\/li>\n<li>\u8ba1\u7b97&#xff1a; \u5206\u522b\u4ece\u5de6\u5f80\u53f3\u548c\u4ece\u53f3\u5f80\u5de6\u6c42\u51fa\u7d2f\u4e58\u7ed3\u679c\u3002<\/li>\n<h4>\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Solution<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    vector<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token function\">productExceptSelf<\/span><span class=\"token punctuation\">(<\/span>vector<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token operator\">&amp;<\/span> nums<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> n <span class=\"token operator\">&#061;<\/span> nums<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        vector<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token function\">ans<\/span><span class=\"token punctuation\">(<\/span>n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        vector<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token function\">ret_l<\/span><span class=\"token punctuation\">(<\/span>n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><span class=\"token function\">ret_r<\/span><span class=\"token punctuation\">(<\/span>n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        ret_l<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span>ret_r<span class=\"token punctuation\">[<\/span>n<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>i <span class=\"token operator\">&lt;<\/span> n<span class=\"token punctuation\">;<\/span>i<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">)<\/span> ret_l<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> ret_l<span class=\"token punctuation\">[<\/span>i<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">*<\/span> nums<span class=\"token punctuation\">[<\/span>i<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> n<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">;<\/span>i <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span>i<span class=\"token operator\">&#8212;<\/span><span class=\"token punctuation\">)<\/span> ret_r<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> ret_r<span class=\"token punctuation\">[<\/span>i<span class=\"token operator\">&#043;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">*<\/span> nums<span class=\"token punctuation\">[<\/span>i<span class=\"token operator\">&#043;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span>i <span class=\"token operator\">&lt;<\/span> n<span class=\"token punctuation\">;<\/span>i<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">)<\/span> ans<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> ret_l<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">*<\/span> ret_r<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> ans<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u23f1\ufe0f \u65f6\u95f4\u590d\u6742\u5ea6&#xff1a;<span class=\"katex--inline\"><span class=\"katex\"><span class=\"katex-mathml\"><\/p>\n<p>         O<\/p>\n<p>         (<\/p>\n<p>         N<\/p>\n<p>         )<\/p>\n<p>        O(N)<\/p>\n<p>     <\/span><span class=\"katex-html\"><span class=\"base\"><span class=\"strut\" style=\"height: 1em;vertical-align: -0.25em\"><\/span><span class=\"mord mathnormal\" style=\"margin-right: 0.0278em\">O<\/span><span class=\"mopen\">(<\/span><span class=\"mord mathnormal\" style=\"margin-right: 0.109em\">N<\/span><span class=\"mclose\">)<\/span><\/span><\/span><\/span><\/span> &#x1f3e0; \u7a7a\u95f4\u590d\u6742\u5ea6&#xff1a;<span class=\"katex--inline\"><span class=\"katex\"><span class=\"katex-mathml\"><\/p>\n<p>         O<\/p>\n<p>         (<\/p>\n<p>         N<\/p>\n<p>         )<\/p>\n<p>        O(N)<\/p>\n<p>     <\/span><span class=\"katex-html\"><span class=\"base\"><span class=\"strut\" style=\"height: 1em;vertical-align: -0.25em\"><\/span><span class=\"mord mathnormal\" style=\"margin-right: 0.0278em\">O<\/span><span class=\"mopen\">(<\/span><span class=\"mord mathnormal\" style=\"margin-right: 0.109em\">N<\/span><span class=\"mclose\">)<\/span><\/span><\/span><\/span><\/span><\/p>\n<h2>\u89e3\u6cd5\u4e8c&#xff1a;\u53cc\u6307\u9488\u4f18\u5316&#xff08;\u9996\u5c3e\u6307\u9488&#xff09;<\/h2>\n<h4>\u4f18\u5316\u601d\u8def<\/h4>\n<p>\u524d\u540e\u7f00\u4e58\u79ef\u6570\u7ec4\u53ea\u7528\u4e86\u4e00\u6b21&#xff0c;\u8003\u8651\u7528\u53cc\u6307\u9488\u8fb9\u904d\u5386\u8fb9\u6c42&#xff0c;\u4ece\u800c\u4f18\u5316\u7a7a\u95f4\u590d\u6742\u5ea6\u3002\u6211\u4eec\u7528\u53d8\u91cf ret_l &#061; 1,ret_r &#061; 1 \u4ee3\u66ff\u6570\u7ec4&#xff1a;<\/p>\n<p>\u65b9\u6848 A&#xff1a;\u4e24\u6b21\u5faa\u73af<\/p>\n<ul>\n<li>\u7b2c\u4e00\u6b21\u5faa\u73af&#xff1a; \u4ece\u5de6\u5f80\u53f3\u5148\u6c42\u5de6\u4fa7\u4e58\u79ef&#xff0c;ans[i] \u8868\u793a i \u5de6\u4fa7\u5143\u7d20\u4e58\u79ef&#xff0c;\u8fed\u4ee3\u66f4\u65b0 ret_l;<\/li>\n<li>\u7b2c\u4e8c\u6b21\u5faa\u73af&#xff1a; \u4ece\u53f3\u5f80\u5de6\u5bf9 ans[i] \u7ed3\u679c\u53e0\u52a0\u4e0a\u53f3\u4fa7\u4e58\u79ef&#xff0c;\u5e76\u4e0d\u65ad\u66f4\u65b0 ret_r&#xff0c;\u76f4\u5230\u5faa\u73af\u7ed3\u675f\u3002<\/li>\n<\/ul>\n<h4>\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Solution<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    vector<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token function\">productExceptSelf<\/span><span class=\"token punctuation\">(<\/span>vector<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token operator\">&amp;<\/span> nums<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> n <span class=\"token operator\">&#061;<\/span> nums<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        vector<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token function\">ans<\/span><span class=\"token punctuation\">(<\/span>n<span class=\"token punctuation\">,<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> ret_l <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span>ret_r <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span>i <span class=\"token operator\">&lt;<\/span> n<span class=\"token punctuation\">;<\/span>i<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            ans<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> ret_l<span class=\"token punctuation\">;<\/span><br \/>\n            ret_l <span class=\"token operator\">*&#061;<\/span> nums<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> n<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>i <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span>i<span class=\"token operator\">&#8212;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            ans<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">*&#061;<\/span> ret_r<span class=\"token punctuation\">;<\/span><br \/>\n            ret_r <span class=\"token operator\">*&#061;<\/span> nums<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> ans<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u65b9\u6848 B&#xff1a;\u5355\u6b21\u5faa\u73af\u4f18\u5316<\/p>\n<p>\u53ef\u4ee5\u7ee7\u7eed\u5c06\u4e24\u6b21\u5faa\u73af\u548c\u5230\u4e00\u4e2a\u5faa\u73af\u91cc&#xff0c;\u53ea\u9700\u8981\u7ef4\u62a4\u524d\u540e\u4e24\u6307\u9488 i,j \u5206\u522b\u4ece\u4e24\u4fa7\u5411\u53e6\u4e00\u4fa7\u5de5\u4f5c\u5373\u53ef\u3002<\/p>\n<h4>\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Solution<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    vector<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token function\">productExceptSelf<\/span><span class=\"token punctuation\">(<\/span>vector<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token operator\">&amp;<\/span> nums<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> n <span class=\"token operator\">&#061;<\/span> nums<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        vector<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token function\">ans<\/span><span class=\"token punctuation\">(<\/span>n<span class=\"token punctuation\">,<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> ret_l <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span>ret_r <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span>j <span class=\"token operator\">&#061;<\/span> n<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>i <span class=\"token operator\">&lt;<\/span> n<span class=\"token punctuation\">;<\/span>i<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">,<\/span>j<span class=\"token operator\">&#8212;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            ans<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">*&#061;<\/span> ret_l<span class=\"token punctuation\">;<\/span><br \/>\n            ans<span class=\"token punctuation\">[<\/span>j<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">*&#061;<\/span> ret_r<span class=\"token punctuation\">;<\/span><br \/>\n            ret_l <span class=\"token operator\">*&#061;<\/span> nums<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            ret_r <span class=\"token operator\">*&#061;<\/span> nums<span class=\"token punctuation\">[<\/span>j<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> ans<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u23f1\ufe0f \u65f6\u95f4\u590d\u6742\u5ea6&#xff1a;<span class=\"katex--inline\"><span class=\"katex\"><span class=\"katex-mathml\"><\/p>\n<p>         O<\/p>\n<p>         (<\/p>\n<p>         N<\/p>\n<p>         )<\/p>\n<p>        O(N)<\/p>\n<p>     <\/span><span class=\"katex-html\"><span class=\"base\"><span class=\"strut\" style=\"height: 1em;vertical-align: -0.25em\"><\/span><span class=\"mord mathnormal\" style=\"margin-right: 0.0278em\">O<\/span><span class=\"mopen\">(<\/span><span class=\"mord mathnormal\" style=\"margin-right: 0.109em\">N<\/span><span class=\"mclose\">)<\/span><\/span><\/span><\/span><\/span> &#x1f3e0; \u7a7a\u95f4\u590d\u6742\u5ea6&#xff1a;<span class=\"katex--inline\"><span class=\"katex\"><span class=\"katex-mathml\"><\/p>\n<p>         O<\/p>\n<p>         (<\/p>\n<p>         1<\/p>\n<p>         )<\/p>\n<p>        O(1)<\/p>\n<p>     <\/span><span class=\"katex-html\"><span class=\"base\"><span class=\"strut\" style=\"height: 1em;vertical-align: -0.25em\"><\/span><span class=\"mord mathnormal\" style=\"margin-right: 0.0278em\">O<\/span><span class=\"mopen\">(<\/span><span class=\"mord\">1<\/span><span class=\"mclose\">)<\/span><\/span><\/span><\/span><\/span>&#xff08;\u9664\u8f93\u51fa\u6570\u7ec4\u5916&#xff09;<\/p>\n<h2>\u2764\ufe0f \u6700\u540e<\/h2>\n<p>\u65b0\u4ebaup&#xff0c;\u5982\u6709\u4e0d\u8db3\u8fd8\u8bf7\u591a\u591a\u6307\u6b63&#xff0c;\u6b22\u8fce\u4ea4\u6d41&#xff01; \u5982\u679c\u5185\u5bb9\u5bf9\u4f60\u6709\u5e2e\u52a9\u7684\u8bdd&#xff0c;\u8fd8\u8bf7\u70b9\u8d5e\u652f\u6301\u4e00\u4e0b\u55bd&#x1f64f;&#x1f64f; \u4f60\u7684\u652f\u6301\u662f\u6211\u524d\u8fdb\u7684\u6700\u5927\u52a8\u529b&#xff01;&#xff01; up\u6b63\u5728\u66f4\u65b0\u529b\u6263hot100\u7684\u9898\u76ee&#xff0c;\u6709\u9700\u8981\u7684\u670b\u53cb\u6b22\u8fce\u70b9\u8d5e\u3001\u6536\u85cf\u52a0\u5173\u6ce8\u54e6&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem:238. \u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef \u9898\u76ee\u63cf\u8ff0<br \/>\n\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums&#xff0c;\u8fd4\u56de \u6570\u7ec4 answer &#xff0c;\u5176\u4e2d answer[i] \u7b49\u4e8e nums \u4e2d\u9664\u4e86 nums[i] \u4e4b\u5916\u5176\u4f59\u5404\u5143\u7d20\u7684\u4e58\u79ef \u3002<br \/>\n\u9898\u76ee\u6570\u636e \u4fdd\u8bc1 \u6570\u7ec4 nums\u4e4b\u4e2d\u4efb\u610f\u5143\u7d20\u7684\u5168\u90e8\u524d\u7f00\u5143\u7d20\u548c\u540e\u7f00\u7684\u4e58\u79ef\u90fd\u5728 32 \u4f4d \u6574\u6570\u8303\u56f4\u5185\u3002<br \/>\n\u8bf7 \u4e0d\u8981\u4f7f\u7528\u9664\u6cd5&#xff0c;\u4e14\u5728 O(n) \u65f6\u95f4\u590d\u6742\u5ea6\u5185\u5b8c\u6210\u6b64\u9898\u3002<br \/>\n\u793a\u4f8b1&#xff1a; \u8f93\u5165&#xff1a; nums  [1,2<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,2692,1813,427],"topic":[],"class_list":["post-68107","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-leetcode","tag-1813","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u529b\u6263Problem:238. \u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef\u3011\u524d\u540e\u7f00\u4e58\u6cd5 + \u53cc\u6307\u9488\u4f18\u5316 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/68107.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u529b\u6263Problem:238. \u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef\u3011\u524d\u540e\u7f00\u4e58\u6cd5 + \u53cc\u6307\u9488\u4f18\u5316 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Problem:238. \u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef \u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums&#xff0c;\u8fd4\u56de \u6570\u7ec4 answer &#xff0c;\u5176\u4e2d answer[i] \u7b49\u4e8e nums \u4e2d\u9664\u4e86 nums[i] \u4e4b\u5916\u5176\u4f59\u5404\u5143\u7d20\u7684\u4e58\u79ef \u3002 \u9898\u76ee\u6570\u636e \u4fdd\u8bc1 \u6570\u7ec4 nums\u4e4b\u4e2d\u4efb\u610f\u5143\u7d20\u7684\u5168\u90e8\u524d\u7f00\u5143\u7d20\u548c\u540e\u7f00\u7684\u4e58\u79ef\u90fd\u5728 32 \u4f4d \u6574\u6570\u8303\u56f4\u5185\u3002 \u8bf7 \u4e0d\u8981\u4f7f\u7528\u9664\u6cd5&#xff0c;\u4e14\u5728 O(n) \u65f6\u95f4\u590d\u6742\u5ea6\u5185\u5b8c\u6210\u6b64\u9898\u3002 \u793a\u4f8b1&#xff1a; \u8f93\u5165&#xff1a; nums [1,2\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/68107.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-29T10:17:20+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/68107.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/68107.html\",\"name\":\"\u3010\u529b\u6263Problem:238. \u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef\u3011\u524d\u540e\u7f00\u4e58\u6cd5 + \u53cc\u6307\u9488\u4f18\u5316 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-29T10:17:20+00:00\",\"dateModified\":\"2026-01-29T10:17:20+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/68107.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/68107.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/68107.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u529b\u6263Problem:238. \u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef\u3011\u524d\u540e\u7f00\u4e58\u6cd5 + \u53cc\u6307\u9488\u4f18\u5316\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010\u529b\u6263Problem:238. \u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef\u3011\u524d\u540e\u7f00\u4e58\u6cd5 + \u53cc\u6307\u9488\u4f18\u5316 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/68107.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u529b\u6263Problem:238. \u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef\u3011\u524d\u540e\u7f00\u4e58\u6cd5 + \u53cc\u6307\u9488\u4f18\u5316 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Problem:238. \u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef \u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums&#xff0c;\u8fd4\u56de \u6570\u7ec4 answer &#xff0c;\u5176\u4e2d answer[i] \u7b49\u4e8e nums \u4e2d\u9664\u4e86 nums[i] \u4e4b\u5916\u5176\u4f59\u5404\u5143\u7d20\u7684\u4e58\u79ef \u3002 \u9898\u76ee\u6570\u636e \u4fdd\u8bc1 \u6570\u7ec4 nums\u4e4b\u4e2d\u4efb\u610f\u5143\u7d20\u7684\u5168\u90e8\u524d\u7f00\u5143\u7d20\u548c\u540e\u7f00\u7684\u4e58\u79ef\u90fd\u5728 32 \u4f4d \u6574\u6570\u8303\u56f4\u5185\u3002 \u8bf7 \u4e0d\u8981\u4f7f\u7528\u9664\u6cd5&#xff0c;\u4e14\u5728 O(n) \u65f6\u95f4\u590d\u6742\u5ea6\u5185\u5b8c\u6210\u6b64\u9898\u3002 \u793a\u4f8b1&#xff1a; \u8f93\u5165&#xff1a; nums [1,2","og_url":"https:\/\/www.wsisp.com\/helps\/68107.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-29T10:17:20+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/68107.html","url":"https:\/\/www.wsisp.com\/helps\/68107.html","name":"\u3010\u529b\u6263Problem:238. \u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef\u3011\u524d\u540e\u7f00\u4e58\u6cd5 + \u53cc\u6307\u9488\u4f18\u5316 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-29T10:17:20+00:00","dateModified":"2026-01-29T10:17:20+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/68107.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/68107.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/68107.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u529b\u6263Problem:238. \u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef\u3011\u524d\u540e\u7f00\u4e58\u6cd5 + \u53cc\u6307\u9488\u4f18\u5316"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/68107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=68107"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/68107\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=68107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=68107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=68107"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=68107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}