My clear and smart c++ solution,32 ms.


  • 1
    M
    class NumArray {
    public:
        vector<int> res;
        NumArray(vector<int> &nums) {
            res = nums;
            for(int i = 1; i < res.size(); ++i){
                res[i] += res[i-1];
            }
        }
    
        int sumRange(int i, int j) {
            if(i < 0 || j > res.size() - 1 || i > j) return 0;
            int ival = res[i] - (i > 0 ? (res[i - 1]) : 0);
            return res[j] - res[i] + ival;
        }
    };

  • 0
    E
    This post is deleted!

Log in to reply
 

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