前缀和 概念与区间和
有勇气的牛排
27
算法
2025-08-10 22:19:17
前缀和 思路
1 前缀和的定义
定一前缀和数组 prefix
:
prefix[i]=nums[0]+nums[1]+...+nums[i-1]
也就是说:
prefix[0] = 0 (空数组的和)
prefix[1] = nums[0]
prefix[2] = nums[0] + nums[1]
...
prefix[i] = nums[0] + nums[1] + ... + nums[i-1]
2 区间和的目标
要求:
sumRange(left, right)=nums[left]+nums[left+1]+...+nums[right]
3 用前缀和表示
观察 prefix[right+1]
和prefix[left]
prefix[right+1] = nums[0]+nums[1]+...+nums[right]
prefix[left] = nums[0]+nums[1]+...+nums[left-1]
两者相减
prefix[right+1]-prefix[left]
= (nums[0]+nums[1]+...+nums[right])-(nums[0]+nums[1]+...+nums[left-1])
= nums[left]+nums[left+1]+...+nums[right] # left-1之前的全部抵消
即:prefix[right+1]-prefix[left]
sumRange(left,right)=nums[left]+nums[left+1]+⋯+nums[right]
= prefix[right+1]-prefix[left]
sumRange(left,right)=nums[left]+nums[left+1]+⋯+nums[right]=prefix[right+1]−prefix[left]
4 举个例子
数组:nums = [-2, 0, 3, -5, 2, -1]
前缀和:prefix_sum = [0, -2, -2, 1, -4, -2, -3]
题目案例
题目:
sumRange(2, 5)
= 3-5+2-1
= prefix_sum[6] - prefix_sum[2]
= (-3)-(-2)
= -1
时间复杂度:
空间复杂度:O(n)
总结
公式成立的原理就是 前缀和的抵消:
前缀和保存的是 从头到某个位置的和,两个前缀和相减,刚好抵消掉前面不需要的部分,留下的就是区间和。
<h1><a id="__0"></a>前缀和 思路</h1>
<h2><a id="1__2"></a>1 前缀和的定义</h2>
<p>定一前缀和数组 <code>prefix</code>:</p>
<p><code>prefix[i]=nums[0]+nums[1]+...+nums[i-1]</code></p>
<p>也就是说:</p>
<pre><div class="hljs"><code class="lang-shell">prefix[0] = 0 (空数组的和)
prefix[1] = nums[0]
prefix[2] = nums[0] + nums[1]
...
prefix[i] = nums[0] + nums[1] + ... + nums[i-1]
</code></div></pre>
<h2><a id="2__18"></a>2 区间和的目标</h2>
<p>要求:</p>
<p><code>sumRange(left, right)=nums[left]+nums[left+1]+...+nums[right]</code></p>
<h2><a id="3__24"></a>3 用前缀和表示</h2>
<p>观察 <code>prefix[right+1]</code>和<code>prefix[left]</code></p>
<pre><div class="hljs"><code class="lang-shell">prefix[right+1] = nums[0]+nums[1]+...+nums[right]
prefix[left] = nums[0]+nums[1]+...+nums[left-1]
</code></div></pre>
<p>两者相减</p>
<pre><div class="hljs"><code class="lang-shell">prefix[right+1]-prefix[left]
= (nums[0]+nums[1]+...+nums[right])-(nums[0]+nums[1]+...+nums[left-1])
= nums[left]+nums[left+1]+...+nums[right] # left-1之前的全部抵消
</code></div></pre>
<p>即:<code>prefix[right+1]-prefix[left]</code></p>
<pre><code class="lang-latex">sumRange(left,right)=nums[left]+nums[left+1]+⋯+nums[right]
= prefix[right+1]-prefix[left]
</code></pre>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mtable><mtr><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow><mi>s</mi><mi>u</mi><mi>m</mi><mi>R</mi><mi>a</mi><mi>n</mi><mi>g</mi><mi>e</mi><mo>(</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo separator="true">,</mo><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi><mo>)</mo></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow><mrow></mrow><mo>=</mo><mi>n</mi><mi>u</mi><mi>m</mi><mi>s</mi><mo>[</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>]</mo><mo>+</mo><mi>n</mi><mi>u</mi><mi>m</mi><mi>s</mi><mo>[</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>+</mo><mn>1</mn><mo>]</mo><mo>+</mo><mo>⋯</mo><mo>+</mo><mi>n</mi><mi>u</mi><mi>m</mi><mi>s</mi><mo>[</mo><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi><mo>]</mo></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow><mrow></mrow><mo>=</mo><mi>p</mi><mi>r</mi><mi>e</mi><mi>f</mi><mi>i</mi><mi>x</mi><mo>[</mo><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi><mo>+</mo><mn>1</mn><mo>]</mo><mo>−</mo><mi>p</mi><mi>r</mi><mi>e</mi><mi>f</mi><mi>i</mi><mi>x</mi><mo>[</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>]</mo></mrow></mstyle></mtd></mtr></mtable></mrow><annotation encoding="application/x-tex">\begin{aligned}
sumRange(left,right)&=nums[left]+nums[left+1]+⋯+nums[right] \\
&= prefix[right+1]-prefix[left]
\end{aligned}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:1.7500000000000002em;"></span><span class="strut bottom" style="height:3.0000000000000004em;vertical-align:-1.2500000000000002em;"></span><span class="base"><span class="mord"><span class="mtable"><span class="col-align-r"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.7500000000000002em;"><span style="top:-3.91em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord mathit">s</span><span class="mord mathit">u</span><span class="mord mathit">m</span><span class="mord mathit" style="margin-right:0.00773em;">R</span><span class="mord mathit">a</span><span class="mord mathit">n</span><span class="mord mathit" style="margin-right:0.03588em;">g</span><span class="mord mathit">e</span><span class="mopen">(</span><span class="mord mathit" style="margin-right:0.01968em;">l</span><span class="mord mathit">e</span><span class="mord mathit" style="margin-right:0.10764em;">f</span><span class="mord mathit">t</span><span class="mpunct">,</span><span class="mord mathit" style="margin-right:0.02778em;">r</span><span class="mord mathit">i</span><span class="mord mathit" style="margin-right:0.03588em;">g</span><span class="mord mathit">h</span><span class="mord mathit">t</span><span class="mclose">)</span></span></span><span style="top:-2.41em;"><span class="pstrut" style="height:3em;"></span><span class="mord"></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:1.2500000000000002em;"></span></span></span></span><span class="col-align-l"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.7500000000000002em;"><span style="top:-3.91em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"></span><span class="mrel">=</span><span class="mord mathit">n</span><span class="mord mathit">u</span><span class="mord mathit">m</span><span class="mord mathit">s</span><span class="mopen">[</span><span class="mord mathit" style="margin-right:0.01968em;">l</span><span class="mord mathit">e</span><span class="mord mathit" style="margin-right:0.10764em;">f</span><span class="mord mathit">t</span><span class="mclose">]</span><span class="mbin">+</span><span class="mord mathit">n</span><span class="mord mathit">u</span><span class="mord mathit">m</span><span class="mord mathit">s</span><span class="mopen">[</span><span class="mord mathit" style="margin-right:0.01968em;">l</span><span class="mord mathit">e</span><span class="mord mathit" style="margin-right:0.10764em;">f</span><span class="mord mathit">t</span><span class="mbin">+</span><span class="mord mathrm">1</span><span class="mclose">]</span><span class="mbin">+</span><span class="minner">⋯</span><span class="mbin">+</span><span class="mord mathit">n</span><span class="mord mathit">u</span><span class="mord mathit">m</span><span class="mord mathit">s</span><span class="mopen">[</span><span class="mord mathit" style="margin-right:0.02778em;">r</span><span class="mord mathit">i</span><span class="mord mathit" style="margin-right:0.03588em;">g</span><span class="mord mathit">h</span><span class="mord mathit">t</span><span class="mclose">]</span></span></span><span style="top:-2.41em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"></span><span class="mrel">=</span><span class="mord mathit">p</span><span class="mord mathit" style="margin-right:0.02778em;">r</span><span class="mord mathit">e</span><span class="mord mathit" style="margin-right:0.10764em;">f</span><span class="mord mathit">i</span><span class="mord mathit">x</span><span class="mopen">[</span><span class="mord mathit" style="margin-right:0.02778em;">r</span><span class="mord mathit">i</span><span class="mord mathit" style="margin-right:0.03588em;">g</span><span class="mord mathit">h</span><span class="mord mathit">t</span><span class="mbin">+</span><span class="mord mathrm">1</span><span class="mclose">]</span><span class="mbin">−</span><span class="mord mathit">p</span><span class="mord mathit" style="margin-right:0.02778em;">r</span><span class="mord mathit">e</span><span class="mord mathit" style="margin-right:0.10764em;">f</span><span class="mord mathit">i</span><span class="mord mathit">x</span><span class="mopen">[</span><span class="mord mathit" style="margin-right:0.01968em;">l</span><span class="mord mathit">e</span><span class="mord mathit" style="margin-right:0.10764em;">f</span><span class="mord mathit">t</span><span class="mclose">]</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:1.2500000000000002em;"></span></span></span></span></span></span></span></span></span></span></p>
<h2><a id="4__55"></a>4 举个例子</h2>
<p>数组:nums = [-2, 0, 3, -5, 2, -1]<br />
前缀和:prefix_sum = [0, -2, <strong>-2</strong>, 1, -4, -2, <strong>-3</strong>]</p>
<p>题目案例</p>
<pre><div class="hljs"><code class="lang-shell">题目:
sumRange(2, 5)
= 3-5+2-1
= prefix_sum[6] - prefix_sum[2]
= (-3)-(-2)
= -1
</code></div></pre>
<p>时间复杂度:</p>
<ul>
<li>初始化:O(n)</li>
<li>每次查询:O(1)</li>
</ul>
<p>空间复杂度:O(n)</p>
<h2><a id="_78"></a>总结</h2>
<p>公式成立的原理就是 <strong>前缀和的抵消</strong>:<br />
前缀和保存的是 <strong>从头到某个位置的和</strong>,两个前缀和相减,刚好抵消掉前面不需要的部分,留下的就是区间和。</p>
评论区