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). 
 
Time: O(N)
 
class Solution {
public int minSubArrayLen(int s, int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
} int res = Integer.MAX_VALUE, start = 0, sum = 0;
for(int i = 0; i < nums.length; i++) {
sum += nums[i];
while (start <= i && sum >= s) {
res = Math.min(res, i - start + 1);
sum -= nums[start++];
}
}
return res == Integer.MAX_VALUE ? 0 : res;
}
}

[LC] 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. 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 ...

  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. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

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

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

  9. 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. Java 14 令人期待的 5 大新特性,打包工具终于要来了

    随着新的 Java 发布生命周期的到来,新版本预计将于 2020 年 3 月发布,本文将对其中的 5 个主要特性作些概述. Java 13刚刚发布给开发人员使用不久,最新版本的JDK于2019年9月发 ...

  2. 17.3.10--->关于数值溢出问题

    取值范围: short.int.long 占用的字节数不同,所能表示的数值范围也不同.以32位平台为例,下面是它们的取值范围: 数据类型                             所占字 ...

  3. Java static特性

    static 表示是静态的 特点是:可以用类直接访问. 属于类, 在类加载时就有 因此static方法不能访问成员的 但是成员的可以访问静态的 所有对象可以共享. 因此常常用作工具,比如Math.PI ...

  4. 第二季第十一天 html5语义化标签 css透明度

    span不能设置宽高背景 HTML5语义化标签 <section>标签所包裹的是有一组相似的主题的内容,可以用这个标签来实现文章的章节.标签式对话框中的各种标签页等类似的功能. <s ...

  5. PID<->Port[转]

    //Netstat -anb #include <Windows.h> #include "Psapi.h" #include <Iprtrmib.h> # ...

  6. 初次运行Git前的配置

    初次运行Git前的配置 一.初次运行 Git 前的配置 一般在新的系统上,我们都需要先配置下自己的 Git 工作环境.配置工作只需一次,以后升级时还会沿用现在的配置.当然,如果需要,你随时可以用相同的 ...

  7. typescript-学习使用ts-2

    解构赋值 数组解构 let input = [1, 2]; let [first, second] = input; console.log(first); // outputs 1 console. ...

  8. coursera课程视频

    #!/usr/bin/env python # coding=utf-8 import urllib import urllib2 import cookielib def setcookie(ena ...

  9. 内网部署Docker版本Gitlab

    Gitlab部署: 1. 还原备份文件后记得拷贝gitlab-secrets.json,不然会遇到500错误 2. 下载Docker以及依赖项rpm包 3. 在外网机器下载镜像 a. 拉取——Dock ...

  10. 回顾vim,ftp

    常用服务器 ftp,ssh FTP是文件传输协议的简称,文传协议,用于internet上控制文件的双向传输 它也是一个应用程序,,基于不同的操作系统有不同的FTP应用程序,都遵循同一种协议以传输文件. ...