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. SEO初步学习之影响网站排名的因素

    本文介绍一些比较明显的因素,一些隐藏较深的原因还有待发掘: 1.采集网站内容,即抄袭其他网站的内容. 2.新站上传后建议不要有大的改动. 3.标题频繁修改. 4.大量投放垃圾外链. 5.不做友链,交友 ...

  2. 计算机网络(7): 传输层TCP和UDP以及TCP的工作方式

    UDP:无连接:不保证可靠:面向报文的: TCP:面向连接:提供可靠交付:面向字节流(把应用层的数据分包,每个包装一些字节:不关心应用层给的包多大,而是根据网络状况,窗口大小决定) TCP报文: 序号 ...

  3. 深入分析Java反射(七)-简述反射调用的底层实现

    前提 Java反射的API在JavaSE1.7的时候已经基本完善,但是本文编写的时候使用的是Oracle JDK11,因为JDK11对于sun包下的源码也上传了,可以直接通过IDE查看对应的源码和进行 ...

  4. 相信301跳转大家都知道 rewrite

    相信301跳转大家都知道,这样有利于权重集中,但是我在.htaccess文件写上: RewriteEngine on rewriteCond %{http_host} ^phpddt.com [NC] ...

  5. 吴裕雄--天生自然 JAVA开发学习:基础语法

    package test; public class temp { /* 第一个Java程序 * 它将打印字符串 Hello World */ public static void main(Stri ...

  6. c语言中assert的用法

    /************************************************************************* > File Name: assert.c ...

  7. 套接字详解(socket)

    用户认为的信息之间传输只是建立以两个应用程序上,实际上在TCP连接中是靠套接字来作为他们连接的桥梁. 那么什么是套接字呢? TCP用主机的IP地址加上主机上的端口号作为TCP连接的端点,这种端点就叫做 ...

  8. shell时间函数

    function getlastday(){ if [ $# -lt 2 ]; then echo "usage: getlastday month dayofweek" echo ...

  9. 浮动( Floats )

    浮动( Float )概述 浮动和文字环绕 浮动框就是一个框在当前行被向左或向右挪动(偏移),它不在常规流中,浮动框由浮动元素的框组成. 浮动框( 'float', 'floated' or 'flo ...

  10. sqlite如何避免重复建表(获取已经存在的表)

    找到已经存在的所有表,手动判断是否需要建表 SELECT name FROM SQLITE_MASTER WHERE type='table'ORDER BY name" 建表时sqlite ...