{"id":7963,"date":"2024-03-29T15:04:45","date_gmt":"2024-03-29T07:04:45","guid":{"rendered":"https:\/\/white-night.club\/?p=7963"},"modified":"2024-03-29T15:13:17","modified_gmt":"2024-03-29T07:13:17","slug":"ad169","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2024\/03\/29\/ad169\/","title":{"rendered":"303. \u533a\u57df\u548c\u68c0\u7d22 &#8211; \u6570\u7ec4\u4e0d\u53ef\u53d8"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/range-sum-query-immutable\/description\/\">https:\/\/leetcode.cn\/problems\/range-sum-query-immutable\/description\/<\/a><\/p>\n\n\n\n<p>man,what can I say?prefix sums out.<\/p>\n\n\n\n<p>\u592a\u4e45\u6ca1\u7528go\uff0c\u53ea\u8bb0\u5f97\u4e2amake\u3002\u6700\u540e\u8fd8\u662f\u7528\u56de\u4e86cpp\u3002\u5355\u7eaf\u7b97\u524d\u7f00\u548c\u5012\u662f\u6ca1\u5565\uff0c\u53cd\u800c\u662fvector\u7528\u6cd5\u592a\u4e45\u6ca1\u7528\u8bb0\u4e0d\u6e05\u4e86\uff0c\u4ee4\u4eba\u611f\u6168\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">C++<\/span><span role=\"button\" tabindex=\"0\" data-code=\"class NumArray {\npublic:\n    vector&lt;int&gt; numArray;\n    NumArray(vector&lt;int&gt;&amp; nums) {\n        int len=nums.size();\n        numArray.resize(len+1);\n        for(int i=0;i&lt;len;i++)\n        {\n            numArray[i+1]=nums[i]+numArray[i];\n        }\n    }\n    \n    int sumRange(int left, int right) {\n        return numArray[right+1]-numArray[left];\n    }\n};\n\n\/**\n * Your NumArray object will be instantiated and called as such:\n * NumArray* obj = new NumArray(nums);\n * int param_1 = obj-&gt;sumRange(left,right);\n *\/\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">NumArray<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    vector&lt;<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\">&gt; numArray;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">NumArray<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #4EC9B0\">vector<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\">&gt;<\/span><span style=\"color: #569CD6\">&amp;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> len=<\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">size<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #9CDCFE\">numArray<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">resize<\/span><span style=\"color: #D4D4D4\">(len+<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> i=<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">;i&lt;len;i++)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #9CDCFE\">numArray<\/span><span style=\"color: #D4D4D4\">[i+<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]=<\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">[i]+<\/span><span style=\"color: #9CDCFE\">numArray<\/span><span style=\"color: #D4D4D4\">[i];<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">sumRange<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">left<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">right<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">numArray<\/span><span style=\"color: #D4D4D4\">[right+<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]-<\/span><span style=\"color: #9CDCFE\">numArray<\/span><span style=\"color: #D4D4D4\">[left];<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/**<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"> * Your NumArray object will be instantiated and called as such:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"> * NumArray* obj = new NumArray(nums);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"> * int param_1 = obj-&gt;sumRange(left,right);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"> *\/<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u662f\u53ef\u80fd\u5c31\u662f\u6700\u540e\u662fright+1\u53bb\u51cfleft\uff0cright\u8981\u591a\u4e00\u9879\u624d\u662f\u6c42[left,right]\u533a\u95f4\u7684\u548c\u3002\u6bd4\u5982\u6c42[1,3]\uff1a<\/p>\n\n\n\n<ul>\n<li>[right+1]\uff1a0+1+2+3<\/li>\n\n\n\n<li>[left]\uff1a0+1<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u7f00\u548c<\/p>\n","protected":false},"author":1,"featured_media":7932,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/7963"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=7963"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/7963\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/7932"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=7963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=7963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=7963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}