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. mysql架构图

    整体架构图 访问控制图

  2. hihoCoder挑战赛28 题目1 : 异或排序

    题目1 : 异或排序 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个长度为 n 的非负整数序列 a[1..n] 你需要求有多少个非负整数 S 满足以下两个条件: ...

  3. 如何使用Gradle的maven-publish将jar包或者war包上传到nexus仓库

    首先,在build.gradle里边声明依赖maven-publish插件: apply plugin: 'maven-publish' 然后,配置项目的信息和和nexus的信息: publishin ...

  4. 总结一下最近用到的技术(2)--JsonSchema和JsonSchemaValidator

    我们最早接触xml的时候会使用一个dtd文件去定义xml里可以有哪些元素和属性等,后来发展到xml schama(是一个xsd文件,在dtd的基础上提供了命名空间等更强大的功能) 现在,RESTful ...

  5. 使用COSBench工具对ceph s3接口进行压力测试--续

    之前写的使用COSBench工具对ceph s3接口进行压力测试是入门,在实际使用是,配置内容各不一样,下面列出 压力脚本是xml格式的,套用UserGuide文档说明,如下 有很多模板的例子,在co ...

  6. Spring Cloud Eureka 高可用注册中心

    参考:<<spring cloud 微服务实战>> 在微服务架构这样的分布式环境中,各个组件需要进行高可用部署. Eureka Server 高可用实际上就是将自己作为服务向其 ...

  7. HTML表格的运用

    表格是用<table>标签定义的,表格被划分为行(使用<标签>),html 难度比C语言,JAVA,Oracle容易,但是要会灵活运用并把命令记住,不断测试一些结果 ,编程还是 ...

  8. 利用反射生成JDK动态代理

    利用反射生成JDK动态代理 在Java的java.lang.reflect包下提供了一个Proxy类和一个InvocationHandler接口,通过使用这个类和接口可以生成JDK动态代理类和动态代理 ...

  9. 最简单,最实用的数据库CHM文档生成工具——DBCHM

    DBCHM支持SqlServer/MySql/Oracle/PostgreSQL等数据库的表列批注维护管理. DBCHM有以下几个功能 表,列的批注可以编辑保存到数据库. 表,列的批注支持通过pdm文 ...

  10. 1.0Tensorflow中出现编译问题的解决方案

    跑简单tf例程的时候遇到这个 sess = tf.Session(),I tensorflow/core/platform/cpu_feature_guard.cc:] Your CPU suppor ...