Xiaohe-LeetCode 303 Range Sum Query-Immutable

(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];
    }
}

 

posted @ 2015-11-22 10:22  CathyXiaohe  阅读(109)  评论(0编辑  收藏  举报