LeetCode – 303. Range Sum Query – Immutable


Given an integer array nums, find the sum of the elements between indices i and j (ij), inclusive.

Example:

Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3  

Note:

  1. You may assume that the array does not change.
  2. There are many calls to sumRange function.

When I saw this question, I was completely stumped. It wasn't just about writing an algorithm; it was about writing a small program.

public class NumArray { public int[] sums; public NumArray(int[] nums) { if(nums.length == 0) return; sums = new int[nums.length]; sums[0] = nums[0]; for(int i=1;i 

This websiteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:

Original author:Jake Tao,source:"LeetCode – 303. Range Sum Query – Immutable"

139
0 0 139

Further Reading

Post a reply

Log inYou can only comment after that.
Share this page
Back to top