【leetcode】Minimum Size Subarray Sum(middle)

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 length under the problem constraint.

思路:用start, end两个游标来记录范围,sum < s end就向后走, s >= sum start就向后走。

我写的代码没有大神的逻辑清晰,先上大神的。

int minSubArrayLen(int s, vector<int>& nums) {
    int firstPos = 0, sum = 0, minLength = INT_MAX;
    for(int i = 0; i<nums.size(); i++) {   //i即end游标 对所有end游标循环
        sum += nums[i];
        while(sum >= s) {   //对每个end游标的start游标循环 firstPos即为start游标 只有s >= sum 时才把start向后移
            minLength = min(minLength, i - firstPos + 1);
            sum -= nums[firstPos++];
        }
    }

    return minLength == INT_MAX? 0 : minLength;  //没找到s >= sum 时返回0
  }

我的代码乱一点,但是也AC了。

int minSubArrayLen(int s, vector<int>& nums) {
        int start = 0, end = 0;
        int sum = 0;
        int minLength = nums.size() + 1;
        while(end <= nums.size()) //有等于是因为结尾到最后面时 起始点还可能移动
        {
            if(sum < s)
            {
                if(end == nums.size()) break;
                sum += nums[end++];
            }
            else
            {
                minLength = (minLength < (end - start)) ? minLength : (end - start);
                sum -= nums[start++];
            }
        }
        minLength = (minLength == nums.size() + 1) ? 0 : minLength; //没找到符合条件的子序列 返回0
        return minLength;
    }
时间: 2024-10-13 15:37:06

【leetcode】Minimum Size Subarray Sum(middle)的相关文章

【Leetcode】Minimum Size Subarray Sum

题目链接:https://leetcode.com/problems/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

【leetcode】Container With Most Water(middle)

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

【leetcode】Evaluate Reverse Polish Notation(middle)

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

【leetcode】Linked List Cycle II (middle)

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 思路: 做过,就当复习了. 先用快慢指针判断相交,关键是环开始点的获取. 用上图说明一下,设非环的部分长度为a(包括环的入口点), 环的长度为b(包括环的入口点). 快慢指针相交的位置为绿色的点,距离

【leetcode】Reverse Linked List II (middle)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. 思路: 好困啊,脑子晕晕的. 转了半天AC了.但写的很罗嗦,要学习大神的写法. 注意翻转的写法. 用伪头部 大神14行简洁代码 L

【leetcode】Binary Search Tree Iterator(middle)

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses

【leetcode】Validate Binary Search Tree(middle)

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

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 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