Created
November 2, 2019 17:59
-
-
Save wszdwp/bd031d59d53f009e7c1d754356e88c37 to your computer and use it in GitHub Desktop.
prefix sum template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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