Leetcode 1546 和为目标值的最大数目不重叠非空子数组数目 贪心前缀和

2021-03-31 09:27

阅读:401

标签:set   overlap   else   重叠   leetcode   hashset   targe   最大   大数   

技术图片

  JAVA:

public final int maxNonOverlapping(int[] nums, int target) {
        int re = 0;
        int point = 0;
        while (point  nums.length) {
            int sum = 0;
            Set preSums = new HashSet();
            preSums.add(0);
            while (point  nums.length) {
                sum += nums[point];
                preSums.add(sum);
                if (preSums.contains(sum - target)) {
                    re++;
                    break;
                } else {
                    point++;
                }
            }
            point++;
        }
        return re;
    }

  JS:

var maxNonOverlapping = function (nums, target) {
    let re = 0;
    let point = 0;
    while (point  nums.length) {
        let sums = new Set();
        sums.add(0);
        let sum = 0;
        while (point  nums.length) {
            sum += nums[point];
            if (sums.has(sum - target)) {
                re++;
                break;
            } else {
                sums.add(sum);
                point++;
            }
        }
        point++;
    }
    return re;
};

技术图片

 

Leetcode 1546 和为目标值的最大数目不重叠非空子数组数目 贪心前缀和

标签:set   overlap   else   重叠   leetcode   hashset   targe   最大   大数   

原文地址:https://www.cnblogs.com/niuyourou/p/13562591.html


评论


亲,登录后才可以留言!