首页 > 其他 > 详细

LeetCode "Maximum Size Subarray Sum Equals k"

时间:2016-01-06 20:14:49      阅读:360      评论:0      收藏:0      [点我收藏+]

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!