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. template.js的介绍

    artTemplate 介绍 artTemplate 是新一代 javascript 模板引擎,它采用预编译方式让性能有了质的飞跃,并且充分利用 javascript 引擎特性,使得其性能无论在前端还 ...

  2. node,npm,webpack,vue-cli模块化编程安装流程

    首先什么都不要管,先装环境. pip是万能的!!! 安装node: pip3 install node 安装npm:   pip3 install npm 安装webpack: npm install ...

  3. Dynamics CRM - 利用 JavaScript 打开指定 entity 的新建窗口并传递需要的参数

    由于业务逻辑需要,需要从某个 Entity 的 Record 中弹出其他 Entity 的创建窗口,并将当前 Entity 中的某些值传递到新打开的窗口中,具体的 JS 代码如下: //定义一个参数对 ...

  4. DVWA-目录遍历-文件包含

    开门见山 · 目录遍历 替换成 2. 文件包含可以使用 绝对路径 也可以 3. 可以使用文件包含来包含一个网址,或者是一个shell 远程文件 空字符绕过字符过滤 %00

  5. 备战秋招——C++知识点

    1.字符串的末尾'\0'也算一个字符,一个字节. 2.使用库函数strcpy(a,b)进行拷贝b->a操作,strcpy会从源地址一直往后拷贝,直到遇到'\0'为止.所以拷贝的长度是不定的.如果 ...

  6. SALESGROSSSALES_成本_利润

    //获取成本GETCOST_TMP:NoConcatenateLOAD T_SAL_OUTSTOCK.LE_ID, [T_SAL_OUTSTOCK.LCY CODE], T_SAL_OUTSTOCK. ...

  7. 用c语言实现的几个小项目

    1.参考:Linux系统编程 2.参考:制作简单计算器 3.参考:制作2048小游戏 4.参考:五子棋实现

  8. Oauth2.0详解及安全使用

    引言:刚刚参加工作的时候接到的第一个任务就是接入新浪的联合登录功能,当时新浪用的还是oauth1.0协议.接入的时候没有对oauth协议有过多的了解,只是按照开放平台的接入流程进行开发,当时还在想这么 ...

  9. Python笔记_第二篇_面向过程_第二部分_1.函数

    函数:这个词属于一个数学概念,在编程语言借鉴了这个概念,表现形式是一段程序代码的组合,也叫“程序集”.有过编程基础的人很容易理解这个概念,当我们编写程序越来越多的时候,程序设计大师们会把散乱的程序进行 ...

  10. MinGW,gcc

    http://www.itdaan.com/blog/2018/01/14/6b7b0613ca61d8c0ea06817f5dd2842b.html https://bbs.feng.com/rea ...