(1)This one I used DP.
O(n).Runtime: 3 ms
This is the only one I have can be accepted on leetcode.
public class NumArray {
private int[] acc;//acc[i]:record sum from 0 to i;
public NumArray(int[] nums) {
acc=new int[nums.length+1];
acc[0]=0;
for(int i=1;i<=nums.length;i++)
{
acc[i]=acc[i-1]+nums[i-1];//transition function
}
}
public int sumRange(int i, int j) {
return acc[j+1]-acc[i];
}
}