码迷,mamicode.com
首页 > 其他好文 > 详细

Jan 13 - Range Sum Query; DP?

时间:2016-01-14 16:15:45      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

代码:

public class NumArray {
    private int[] sum;
    //private int[] nums;
    public NumArray(int[] nums) {
        int len = nums.length;
        sum = new int[len];
        if(len != 0){
           //this.nums = nums;
            sum[0] = nums[0];
            for(int i = 1; i < len; i++) sum[i] = sum[i-1] + nums[i];
        }
    }

    public int sumRange(int i, int j) {
        if(i == 0) return sum[j];
        return sum[j]-sum[i-1];
        /*
        if(sum[i][j] != 0) return sum[i][j];
        if(i == j) sum[i][j] = nums[i];
        else if(i == j-1) sum[i][j] = sumRange(i, i) + sumRange(i+1, j);
        else{
            sum[i][j] = sumRange(i, i) + sumRange(i+1, j);
            sum[i][j] = sumRange(i, j-1) + sumRange(j, j);
        }
        return sum[i][j];
        */
    }
}

  

Jan 13 - Range Sum Query; DP?

标签:

原文地址:http://www.cnblogs.com/5683yue/p/5130391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!