My clear cpp code


  • 4
    C
    class NumArray {
    public:
    
        vector<int> sums;
        
        NumArray(vector<int> &nums) 
        {
            sums = vector<int>(nums.size()+1, 0);
            
            for(int i=0; i < nums.size(); i++)
                sums[i+1] = nums[i] + sums[i];
        }
    
        int sumRange(int i, int j) 
        {
            if(sums.size() == 1) return sums[0];
            return sums[j+1] - sums[i];
        }
    };
    
    
    // Your NumArray object will be instantiated and called as such:
    // NumArray 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.