LeetCode() Minimun Size Subarray Sum

别人的代码

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
         int l, r, cum, res = nums.size()+1;
    l = r = cum = 0;
    while ((unsigned int)r < nums.size()) {
        cum += nums[r++];
        while (cum >= s) {
            res = min(res, r-l);
            cum -= nums[l++];
        }
    }
    return res<=nums.size()?res:0;
    }
};

我的 280ms

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        vector<int> res;
        int start=0,end=0,len=INT_MAX;
        for(int end=0;end<nums.size();++end)
        {
             while(sum(nums,s,start,end) && start<=end)
             {
                 (end-start+1 < len)? len=end-start+1:len;
                 start++;
             }
        }
        if(len == INT_MAX)
            return 0;
        return len;
    }
    bool sum(vector<int>& nums,int s,int i,int j)
    {
        for(int k=i;k<=j;++k)
            s=s-nums[k];
        return s<=0;
    }
};
时间: 2024-10-08 17:06:23

LeetCode() Minimun Size Subarray Sum的相关文章

[LeetCode] Minimum Size Subarray Sum 解题思路

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

[LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3, return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is th

Leetcode: Maximum Size Subarray Sum Equals k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3, return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is th

LeetCode &quot;Mininum Size Subarray Sum&quot;

O(n): Sliding window: class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int len = nums.size(); if (len == 0) return 0; int w = INT_MAX; int i = 0, j = 0; int sum = nums[0]; while (i <= j && j < len) { if (sum

LeetCode -- Minimum Size Subarray Sum

Question: Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has th

LeetCode Minimum Size Subarray Sum (最短子序列和)

题意:给一个序列,找出其中一个连续子序列,其和大于s但是所含元素最少.返回其长度.0代表整个序列之和均小于s. 思路:O(n)的方法容易想.就是扫一遍,当子序列和大于s时就一直删减子序列前面的一个元素,直到小于s就停下,继续累加后面的. 1 class Solution { 2 public: 3 int minSubArrayLen(int s, vector<int>& nums) { 4 int ans=0x7fffffff, cnt=0, sum=0; 5 for(int i=

【LeetCode】209. Minimum Size Subarray Sum

Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subar

leetcode_209题——Minimum Size Subarray Sum(两个指针)

Minimum Size Subarray Sum Total Accepted: 10318 Total Submissions: 44504My Submissions Question Solution Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, r

[LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead. Example: Input: s = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: the subarr