Nothing special. Point is, how clean can you do it?
class Solution { public: int maxSubArrayLen(vector<int>& nums, int k) { int n = nums.size(); // Partial Sum vector<int> asum(n); partial_sum(nums.begin(), nums.end(), asum.begin()); int ret = 0; unordered_map<int, int> hm; // asum - min inx hm[0] = -1; for (int i = 0; i < n; i++) { int tgt = asum[i] - k; if (hm.count(tgt)) ret = max(ret, i - hm[tgt]); if (!hm.count(asum[i])) hm[asum[i]] = i; } return ret; } };
LeetCode "Maximum Size Subarray Sum Equals k"
原文:http://www.cnblogs.com/tonix/p/5106613.html