Why my code is runtime error?


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

    // Your NumArray object will be instantiated and called as such:
    // NumArray numArray(nums);
    // numArray.sumRange(0, 1);
    // numArray.sumRange(1, 2);


  • 0

    @FeiPingWang

    You haven't considered the corner case:

    if(nums.empty()) return ;
    

  • 0
    F

    Thanks, you are right!


Log in to reply
 

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