new learner .i do not know how to deal with it.wish for help


  • 0
    X

    class NumArray {
    public:
    NumArray(vector<int> nums) {
    int size=nums.size();
    int*b=new int[size];
    for( int i=0;i<size;i++)
    b[i]=nums[i];
    }

    void update(int i, int val) {
        b[i]=b[i]-nums[i]+val;
        nums[i]=val;
    }
    
    int sumRange(int i, int j) {
        int m=i,n=j;
        if(i==j),return b[i];
        if((j-i)%2==0)
        {
            for(int k=i;k<=m+(n-m)/2;k++)
            {
                b[k]=b[m]+b[m+1];
                m+=2;
                if(m==n),b[k]=b[n];
            }
            sumRange(i,i+(j-i)/2);
        }
        else
        {
            for(int k=m;k<=m+(n-m)/2;k++)
            {
                b[k]=b[m]+b[m+1];
                m+=2;
            }
            sumRange(i,i+(j-i)/2);
        }
    }  
    

    };


Log in to reply
 

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