C# Solution (Beats 98%)


  • 0
    K
    public class NumArray {
        int[] arr;
        
        public NumArray(int[] nums) {
            arr = new int[nums.Count()];
            int currSum = 0;
            for(int i = 0; i < nums.Count(); ++i){
                currSum += nums[i];
                arr[i] = currSum;
            }
        }
    
        public int SumRange(int i, int j) 
        {
            int a = arr[j];
            int b = 0;
            if (i != 0){
                b = arr[i - 1];
            }
            return a - b;
        }
    }
    
    
    // Your NumArray object will be instantiated and called as such:
    // NumArray numArray = new NumArray(nums);
    // numArray.SumRange(0, 1);
    // numArray.SumRange(1, 2);

Log in to reply
 

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