Cpp solution which beats 100% submissions. easy to understand


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

Log in to reply
 

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