Beat 100% c++ solution


  • 6

    class NumArray {

    private:
    vector<int> data;
    

    public:

    NumArray(vector<int> &nums) {
        int sum=0;
        for(int i=0;i<nums.size();i++)
        {
            sum+=nums[i];
            data.push_back(sum);
        }
    }
    
    int sumRange(int i, int j) {
        if(i==0)
            return data[j];
        else
            return data[j]-data[i-1];
    }
    

    };

    // 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.