leetcode-209 长度最小的数组

leetcode-209 长度最小的数组

题目描述:

给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。
参考:负雪明烛

class Solution(object):
    def minSubArrayLen(self, s, nums):
        """
        :type s: int
        :type nums: List[int]
        :rtype: int
        """
        res = float("inf")
        l,r = 0,0
        N = len(nums)
        sumc = 0
        # 两个while的很有趣,只使用n的复杂度完成这个问题,感觉还是要把思维理清,要分步骤,不要一团浆糊
        while r < N:
            sumc += nums[r]
            while sumc >= s:
                res = min(res,r-l+1)
                sumc -= nums[l]
                l += 1
            r += 1
        return res if res<float("inf") else 0

原文地址:https://www.cnblogs.com/curtisxiao/p/11219748.html

时间: 2024-07-31 02:03:31

leetcode-209 长度最小的数组的相关文章

Leetcode 209.长度最小的子数组 By Python

给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = 7, nums = [2,3,1,2,4,3] 输出: 2 解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组. 进阶: 如果你已经完成了O(n) 时间复杂度的解法, 请尝试 O(n log n) 时间复杂度的解法. 思路 滑动窗口法,设立两个指针i,j,判断nums[i:j+1]跟s的大小关系来决定i,j的走向,

LeetCode 209. 长度最小的子数组(Minimum Size Subarray Sum)

题目描述 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = 7, nums = [2,3,1,2,4,3] 输出: 2 解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组. 解题思路 记录当前的连续子数组和,若大于等于s,则以当前子数组的最左端为基准向后遍历,若去掉此数后当前连续子数组和仍大于等于s,就把左端向右移动一位,否则更新当前的最小连续子数组长度. 代码

leetcode 209. 长度最小的子数组 java

题目: 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = 7, nums = [2,3,1,2,4,3] 输出: 2 解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组. 进阶: 如果你已经完成了O(n) 时间复杂度的解法, 请尝试 O(n log n) 时间复杂度的解法. 解题: class Solution { public int minSubArrayLe

LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)

718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. LeetCode718. Maximum Length of Repeated Subarray 示例: 输入: s = 7, nums = [2,3,1,2,4,3] 输出: 2 解释: 子数组 [4,3] 是该条件下的长度最小的连

大于或等于给定值 长度最小的子数组 minimum size subarray sum [209]

leetcode 209 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] ha

长度最小的子数组

给定一个含有?n?个正整数的数组和一个正整数?s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例:? 输入: s = 7, nums = [2,3,1,2,4,3]输出: 2解释: 子数组?[4,3]?是该条件下的长度最小的连续子数组.进阶: 如果你已经完成了O(n) 时间复杂度的解法, 请尝试?O(n log n) 时间复杂度的解法. class Solution { public int minSubArrayLen(int s, i

[Swift]LeetCode209. 长度最小的子数组 | 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

[刷题] LeetCode 209 Minimum Size Subarray Sum

要求 给定一个含有 n 个正整数的数组和一个正整数 s 找出该数组中满足其和 ≥ s 的长度最小的连续子数组 如果不存在符合条件的连续子数组,返回 0 示例 输入:s = 7, nums = [2,3,1,2,4,3] 输出:2 解释:子数组 [4,3] 是该条件下的长度最小的连续子数组 思路 暴力解法(n3) 滑动窗口() 1 class Solution{ 2 public: 3 int minSubArrayLen(int s, vector<int>& nums){ 4 int

leetCode 118. Pascal&#39;s Triangle 数组 (杨辉三角)

118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [      [1],     [1,1],    [1,2,1],   [1,3,3,1],  [1,4,6,4,1] ] 题目大意: 输入行数,输出如上图所示的数组.(杨辉三角) 思路: 用双vector来处理当前行和下一行. 代码如下: cla