C++ clear solution by simply pre-process, no need for explanation I guess


  • 0
    U
    class NumArray {
    private:
        int n;
        vector<int> nums;
        vector<int> sums;
    public:
        NumArray(vector<int> &nums) {
            n = nums.size();
            nums = nums;
            int tmp = 0;
            for (int i = 0; i < n; i++) {
                tmp += nums[i];
                sums.push_back(tmp);
            }
        }
    
        int sumRange(int i, int j) {
            if (i == 0) return sums[j];
            else return (sums[j]-sums[i-1]);
        }
    };
    

Log in to reply
 

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