c++ solution


  • 0
    B
    class NumArray {
    public:
        vector<int> sum;
        NumArray(vector<int> &nums) {
            if(!nums.size()) return;
    
            sum.push_back(nums[0]);
            for(int i=1;i<nums.size();i++){
                sum.push_back(nums[i]+sum[i-1]);
            }
        }
    
        int sumRange(int i, int j) {
            if (i==0) return sum[j];
            return sum[j]-sum[i-1];
        }
    };
    

Log in to reply
 

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