Java solution for java lovers, support Java


  • -2
    N
    int[] nums;
    public NumArray(int[] nums) {
        this.nums = nums;
    }
    
    public int sumRange(int i, int j) {
        int sum = 0;
        for(int k = i; k <= j; k++)
        {
            sum += nums[k];
        }
        return sum;
    }

  • 0
    L

    what's the point of this solution? it's meaningless. As it tagged, this is a DP problem.


  • 1
    M

    Hi,

    Your code is right, but it seems that the code will cause TLE(Time Limited Exception) because it is not efficient. Actually there is no need to calculate the sum every time. Using a DP(Dynamic Programming) could help you to solve the problem.

    Since you are a beginner, there might a lot of things you need to pick up. Looking forward to making progress with you together.

    Regards,
    Levi


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.