Skip to content

Instantly share code, notes, and snippets.

@wszdwp
Created November 2, 2019 17:59
Show Gist options
  • Save wszdwp/bd031d59d53f009e7c1d754356e88c37 to your computer and use it in GitHub Desktop.
Save wszdwp/bd031d59d53f009e7c1d754356e88c37 to your computer and use it in GitHub Desktop.
prefix sum template
/**
* Prefix sums of nums, preSum[i] = sum of range [0, j)
* sum[i, j) = preSum[j] - preSum[i]
*
* @param nums
* @return preSum array
*/
public static int[] getPrefixSum(int[] nums) {
int[] preSum = new int[nums.length + 1];
for (int i = 1; i <= nums.length; i++) {
preSum[i] = preSum[i - 1] + nums[i - 1];
}
return preSum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment