Why is there nobody just use vector to solve this problem?It takes 900ms, and I do not know about segment tree.


  • 1
    M

    It is just another version of the preview problem. It is easy for beginners like this code:

    class NumArray {
    public:
        vector<int> res;
        NumArray(vector<int> &nums) {
            res.push_back(0);
            int sum=0;
            for(int i=0;i<nums.size();i++)
            {
                sum+=nums[i];
                res.push_back(sum);
            }
        }
    
        void update(int i, int val) {
            int dif=val-(res[i+1]-res[i]);
            for(int j=i+1;j<res.size();j++)
                res[j]+=dif;
        }
    
        int sumRange(int i, int j) {
            return res[j+1]-res[i];
        }
    };

  • 0
    W

    Exactly same idea, but got TLE using Python...


Log in to reply
 

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