算法

0. 前缀和求子数组之和

2020-12-21  本文已影响0人  云殊_Tech

source

定义子数组之和:
let sum(i,j) = nums[i]+nums[i+1]...+nums[j]

前缀和 preSum

For given integer array nums of length n, we create a preSum array preSum of length n+1, with following features:

代码

int n = nums.length;
// 前缀和数组
int[] preSum = new int[n + 1];
preSum[0] = 0;
for (int i = 0; i < n; i++)
    preSum[i + 1] = preSum[i] + nums[i];
上一篇 下一篇

猜你喜欢

热点阅读