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: [2,3,1,2,4,3], s = 7
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
s = , nums = [,,,,,]

^
l
r
上边的窗口内所有数字的和 小于 , r 右移 ^ ^
l r
上边的窗口内所有数字的和 + 小于 , r 右移 ^ ^
l r
上边的窗口内所有数字的和 + + 小于 , r 右移 ^ ^
l r
上边的窗口内所有数字的和 + + + 大于等于了 , 记录此时的长度 min = , l 右移 ^ ^
l r
上边的窗口内所有数字的和 + + 小于 , r 右移 ^ ^
l r
上边的窗口内所有数字的和 + + + 大于等于了 , 更新此时的长度 min = , l 右移 ^ ^
l r
上边的窗口内所有数字的和 + + 大于等于了 , 更新此时的长度 min = , l 右移 ^ ^
l r
上边的窗口内所有数字的和 + 小于 , r 右移 ^ ^
l r
上边的窗口内所有数字的和 + + 大于等于了 , 更新此时的长度 min = , l 右移 ^ ^
l r
上边的窗口内所有数字的和 + 大于等于了 , 更新此时的长度 min = , l 右移 ^
r
l
上边的窗口内所有数字的和 小于 , r 右移,结束
class Solution {
public:
int minSubArrayLen(int s, vector<int>& a) {
int slow = 0;
int min_res = INT_MAX;
int n = a.size();
int sum ; for (int i = 0; i < n; i++) {
sum += a[i];
while(sum >= s) {
min_res = std::min(min_res,i+1-slow);
sum-=a[slow++];
}
}
return (min_res != INT_MAX) ? min_res : 0;
}
};

  

 class Solution {
public int minSubArrayLen(int target, int[] a) {
if(a.length==0||a.length==1)
return 0;
int i = 0,j = 0,sum =0 ,min = Integer.MAX_VALUE;
while(j<a.length){
sum+=a[j++];
while(sum>=target){
min = Math.min(min,j-i);
sum-=a[i++];
}
}
return min==Integer.MAX_VALUE?0:min;
} }

209. Minimum Size Subarray Sum(双指针)的更多相关文章

  1. 【刷题-LeetCode】209. Minimum Size Subarray Sum

    Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the m ...

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

    Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...

  3. LeetCode OJ 209. Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  4. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  5. LeetCode 209 Minimum Size Subarray Sum

    Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...

  6. LeetCode 209. Minimum Size Subarray Sum (最短子数组之和)

    Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...

  7. Java for LeetCode 209 Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  8. 209. Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  9. 【Leetcode】209. Minimum Size Subarray Sum

    Question: Given an array of n positive integers and a positive integer s, find the minimal length of ...

随机推荐

  1. jenkins不安装任何插件时,是什么样的

    因为jenkins中插件众多,很多时候大家默认安装的话,会默认安装一堆插件.而很多插件会给jenkins的各个方面去增强它, 一致于不清楚哪些功能是插件提供的,还是jenkins自带的. 所以下面将没 ...

  2. git服务器

    1 关于版本控制版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.有以下三种版本控制系统:1. 本地版本控制系统许多人习惯用复制整个项目目录的方式来保存不同的版本,或许还会 ...

  3. CF510B Fox And Two Dots(搜索图形环)

    B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. 检查mono兼容性的工具MOAM

    mono的迁移工具,可以帮助我们从windows平台迁移到Linux平台,可以用来检测特定的.net的dll或exe程序对mono的兼容性,并能够给出不兼容的方法 项目地址 MoMA 项目介绍 MoM ...

  5. you do not have permission to pull from the repository解决方法

    使用git进行项目的版本管理,换了台电脑,配置了账号和邮箱后,pull一个私有项目的时候,发现一个问题: 原因分析: 这是由于没有设置Gitee的SSH公钥.在未设置SSH公钥的情况下,可以使用git ...

  6. Python 核心编程

    第3章 Python 基础 1.语句和语法: 注释(#): 继续换句话说跨行(\):有两种例外情况一个语句不使用反斜线也可以跨行.在使用闭合操作符时,单一语句可以跨多行,如小括号.中括号,花括号等,另 ...

  7. python类中的self参数和cls参数

    1. self表示一个类的实例对象本身.如果用了staticmethod就无视这个self了,就将这个方法当成一个普通的函数使用了. 2. cls表是这个类本身. # 代码为证 class A(obj ...

  8. Common Gateway Interface Python CGI编程

    https://en.wikipedia.org/wiki/Gateway_(telecommunications) In telecommunications, the term gateway r ...

  9. centos7设置iptables

    https://www.linuxidc.com/Linux/2017-10/147238.htm

  10. LoadRunner-循环

    Edit Runtime Settings ,设置循环次数 在Open Parameter List 里设置循环参数,比如用例为删除notice,每执行一次用例id值不同. 把id替换为参数,并在参数 ...