C++ beat 100% ,just have a look


  • -2
    K
    vector<int> num;
    vector<int> bak;
    NumArray(vector<int> &nums) {
        int res=0;
        for(int i=0;i<nums.size();i++){
            res+=nums[i];
            bak.push_back(nums[i]);
            num.push_back(res);
        }
    }
    
    void update(int i, int val) {
        int res=val-bak[i];
        bak[i]=val;
        for(int j=i;j<num.size();j++)
        num[j]+=res;
    }
    
    int sumRange(int i, int j) {
        if(i==0)return num[j];
        return num[j]-num[i-1];
    }

  • -1
    V

    it's amazing how simple and fast this algorithm is , it's almost the same with other Range sum exercises.


Log in to reply
 

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