O(n^2) C++ solution


  • 0
        int subarraySum(vector<int>& nums, int k) {
            int r=0;
            vector<int> S;
            for(int i = 0; i < nums.size(); i++){
                for_each(S.begin(), S.end(), [&](int& x){ x+=nums[i]; });
                S.push_back(nums[i]);
                for_each(S.begin(), S.end(), [&](int& x){ if(x == k)r++; });
            }
            return r;
        }

Log in to reply
 

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