Jan 13 - Range Sum Query; DP?

代码:

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];
        */
    }
}

  

时间: 2024-08-26 09:11:05

Jan 13 - Range Sum Query; DP?的相关文章

[LeetCode] Range Sum Query 2D - Immutable

Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fact, if you work in computer vision, you may know a name for such an array --- Integral Image. To solve this problem, Stefan has already posted a very e

Range Sum Query 2D - Mutable &amp; Immutable

Range Sum Query 2D - Mutable Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border) is defined by (row1,

LeetCode Range Sum Query - Mutable

原题链接在这里:https://leetcode.com/problems/range-sum-query-mutable/ 题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. The update(i, val) function modifies nums by updating the element at index i to v

2-D range sum query implementation(2-D segment tree)

Google interview question:一个二维数组,有两个方法,一个是update(x,y),更新一个cell的值,一个是query(x1,y1,x2,y2),查询(x1,y1,x2,y2)矩形内所有元素的和. Senario 1. update调用次数远大于query. Senario 2. query调用次数远大于update. Senario 3. 两种方法调用一样多. 对于senario 1,只需要用一个二维数组储存数据,update相应的cell,时间复杂度O(1),qu

[LeetCode] Range Sum Query - Immutable &amp; Range Sum Query 2D - Immutable

Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note: You

304. Range Sum Query 2D - Immutable

/* * 304. Range Sum Query 2D - Immutable * 2016-7-3 by Mingyang * 这个题目自己做了一下同样的dp思路,但是out of boundry,为什么呢? * 因为自己老老实实的把dp的维度跟matrix的维度设成一样的了 * 那我们就把dp的维度再多设一个这样就很好地表示了 * dp[i][j]仅仅表示到i-1,j-1这个点(inclusive)的和的大小 */ class NumMatrix { private int[][] df;

304. Range Sum Query 2D - Immutable java solutions

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, co

LeetCode:Range Sum Query - Immutable - 数组指定区间内的元素和

1.题目名称 Range Sum Query(数组指定区间内的元素和) 2.题目地址 https://leetcode.com/problems/range-sum-query-immutable/ 3.题目内容 英文:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 中文:给定一个数组nums,求出索引i和j之间元素的和,i一定是小于或等于j

leetCode 303. Range Sum Query - Immutable | Dynamic Programming

303. Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note: