标签:
代码:
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]; */ } }
标签:
原文地址:http://www.cnblogs.com/5683yue/p/5130391.html