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. Spring JAR下载地址

    包含3.2版本及以上 http://repo.spring.io/libs-release-local/org/springframework/spring/ 包含从2.0开始的所有版本 http:/ ...

  2. Struts2的Action访问

    ● 示例项目结构 ●  demo1.jsp <%@ page language="java" import="java.util.*" pageEncod ...

  3. 关于软件IntelliJ IDEA的使用技巧(一)

    一,IntelliJ IDEA的下载 点击网址http://www.jetbrains.com/idea/进入官网,点击Download 会出现如下页面 点击Ultimate下的Download,下载 ...

  4. Linux的各个发行版本(一)

    三大流派 1.Slackware SUSE Linux Enterprise Server (SLES) OpenSuse桌面 2.debian 迄今为止最遵循GNU规范的Linux系统 Ubuntu ...

  5. Machine code transfer into assembly code

    #include <stdio.h> const char shell[]="\x0f\x01\xf8\xe8\5\0\0\0\x0f\x01\xf8\x48\xcf" ...

  6. 深信服杯ctf部分wp

    CRYPTO1,NO SOS题目给了一段由.和-构成的密码由于题目提示不是摩斯码,将.和-化为0和1,长度为65位无法与8或7整除,无法转换为ascii,但可以被5整除,猜测为培根密码,将0化为a,1 ...

  7. teb教程1

    http://wiki.ros.org/teb_local_planner/Tutorials/Setup%20and%20test%20Optimization 简介:本部分关于teb怎样优化轨迹以 ...

  8. 在阅读众多的blog中,我学到了什么

    写博客的人,自然会读别人的博客:读博客的人,不一定会写博客.但是这两种人之间的差别是很大的 在最近在一段时间,发现了一个好的博客,通过该博客的友链,发现了新大陆.... 从Jeff Wong开始,到老 ...

  9. HDU-4044 树形背包dp好题

    不会做,题解是参考网上的.感觉这道题是到好题,使得我对树形背包dp更了解了. 有几个注意的点,直接给出代码,题解以及注意点都在注释里了. #include<bits/stdc++.h> u ...

  10. KindEditor在eclipse里的配置方法

    KindEditor介绍: kindEditor是一款国产富文本编辑器,类似fckeditor和目前比较流行的百度Ueditor.其产品官方网站为http://kindeditor.net/ Kind ...