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 subarray [4,3] has the minimal length under the problem constraint.

Follow up:

If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).


class Solution {
public int minSubArrayLen(int s, int[] nums) {
if(nums.length == 0 || nums == null) return 0;
int n = nums.length;
int left = 0, sum = 0, res = Integer.MAX_VALUE;
for(int i=0; i<n; i++){
sum += nums[i];
while(sum >= s){
res = Math.min(res, i-left+1);
sum -= nums[left++];
}
}
return res == Integer.MAX_VALUE ? 0 : res;
}
}

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. 209. Minimum Size Subarray Sum(双指针)

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

  4. [刷题] 209 Minimum Size Subarray Sum

    要求 给定一个含有 n 个正整数的数组和一个正整数 s 找出该数组中满足其和 ≥ s 的长度最小的连续子数组 如果不存在符合条件的连续子数组,返回 0 示例 输入:s = 7, nums = [2,3 ...

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

  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. LeetCode 209 Minimum Size Subarray Sum

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

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

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

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

随机推荐

  1. tushare积分怎么获得 tushare pro 积分充值 积分转让

    本人是做量化投资的,团队转型,换了交易策略,手头有多个离职同事的闲置转让.600分:原价50元,仅需39元1500分:原价150元,仅需109元(售罄)2000分:原价200元,仅需149元5000分 ...

  2. 我的scoi2018

    高一,很尴尬,凉~ -------- 大家好,我是分界线,我弱弱的说本次采用倒序的写作手法 -------- 故事是这样讲的: Day0: 刚刚去那个电科搞的集训,早上才考了一波模拟赛,下午就过来住酒 ...

  3. Activity向Fragment传值

    发送数据 //Activity传值,通过Bundle Bundle bundle = new Bundle(); bundle.putString("MainActivity", ...

  4. Nginx---文档(从入门到精通)

    very good http://tengine.taobao.org/book/index.html

  5. 浅谈无线h5开发

    最近一直在做h5的项目,对h5开发有了自己的理解.首先h5开发并不是指的html5的开发,而是指无线端的web开发,至于为什么叫h5开发,我觉得一方面是因为html5近几年还是挺受关注,另一方面h5在 ...

  6. spring在注解标注的方法上加切面

    之前以为只能在方法签名上加切面,今天发现注解上也能加切面 1.自定义一个注解(任意注解都可以,不一定是自定义的) @Target({ElementType.METHOD}) @Retention(Re ...

  7. QDomDocument::clear()的调用,会导致关闭程序时崩溃!!!

    //读一份xml前,先清理m_Doc[QDomDocument] bool XmlIO::xmlRead(QString &errmsg) { m_mutex.lock(); // m_Doc ...

  8. cocos2D-X Download

    { http://cocos2d-x.org/download https://github.com/cocos2d/cocos2d-x https://github.com/fusijie/Coco ...

  9. Java——类之间的关系

    3.7 类之间的关系 3.7.1 泛化关系 类和类之间的继承关系及接口与接口之间的继承关系. 3.7.2 实现关系 类对接口的实现. 3.7.3 关联关系 类与类之间的连接,一个类可以知道另一个类的属 ...

  10. SQL 删除

    SQL Delete 语句(删除表中的记录) DELETE语句用于删除表中现有记录. SQL DELETE 语句 DELETE 语句用于删除表中的行. SQL DELETE 语法 DELETE FRO ...