给定一个含有 n 个正整数的数组和一个正整数 s , 找到一个最小的连续子数组的长度,使得这个子数组的数字和 ≥  s 。如果不存在符合条件的子数组,返回 0。
举个例子,给定数组 [2,3,1,2,4,3] 和 s = 7,
子数组 [4,3]为符合问题要求的最小长度。
更多练习:
如果你找到了O(n) 解法, 请尝试另一个时间复杂度为O(n log n)的解法。

详见:https://leetcode.com/problems/minimum-size-subarray-sum/description/

Java实现:

方法一:时间复杂度O(nlogn)

class Solution {
public int minSubArrayLen(int s, int[] nums){
int[] sums = new int[nums.length + 1]; for (int i = 1; i < sums.length; i++){
sums[i] = sums[i - 1] + nums[i - 1];
}
int minLen = Integer.MAX_VALUE; for (int i = 0; i < sums.length; i++){
int end = binarySearch(i + 1, sums.length - 1, sums[i] + s, sums);
if (end == sums.length){
break;
}
if (end - i < minLen) {
minLen = end - i;
}
}
return minLen == Integer.MAX_VALUE ? 0 : minLen;
} public int binarySearch(int l, int h, int key, int[] sums){
while (l <= h){
int m = (l + h)>>1;
if (sums[m] >= key)
h = m - 1;
else
l = m + 1; }
return l;
}
}

参考:https://www.cnblogs.com/jimmycheng/p/7477562.html

方法二:时间复杂度O(n)

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

C++实现:

class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int n=nums.size();
if(n==0||nums.empty())
{
return 0;
}
int left=0,right=0,sum=0,res=INT_MAX;
while(right<n)
{
while(sum<s&&right<n)
{
sum+=nums[right++];
}
while(sum>=s)
{
res=min(res,right-left);
sum-=nums[left++];
}
}
return res>n?0:res;
}
};

参考:https://www.cnblogs.com/grandyang/p/4501934.html

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

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

  5. 【Leetcode】209. Minimum Size Subarray Sum

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

  6. 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. **209. Minimum Size Subarray Sum 长度最小的子数组

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

  8. [刷题] 209 Minimum Size Subarray Sum

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

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

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

随机推荐

  1. python菜鸟日记1

    1. 在__init__(self,...)初始化函数中.定义对象的属性,这些属性不用所有写在括号里,括号里的表示定义的时候须要赋初始值的一些属性,而其它不用初始值的属性.能够 直接卸载代码块中,se ...

  2. oracle随机数

    1.从表中随机取记录 select * from (select * from staff order by dbms_random.random) where rownum < 4 表示从ST ...

  3. 读取本地json文件,转出为指定格式json 使用Base64进行string的加密和解密

    读取本地json文件,转出为指定格式json   引用添加Json.Net 引用命名空间 using Newtonsoft.Json //读取自定目录下的json文件StreamReader sr = ...

  4. ImageViewCoverflow

    https://github.com/Bertlk/ImageViewCoverflow https://github.com/dolphinwang/ImageCoverFlow http://ww ...

  5. css3最新版中文参考手册在线浏览

    对于CSS 3.0,它对于我们Web设计人员来说不只是新奇的技术,更重要的是这些全新概念的Web应用给我们的设计开发提高了效率以及更多的无限可能性,我们将不必再依赖图片或者 Javascript 去完 ...

  6. 【iOS系列】-iOS中内存管理

    iOS中创建对象的步骤: 1,分配内存空间,存储对象 2,初始化成员变量 3,返回对象的指针地址 第一:非ARC机制: 1,对象在创建完成的同时,内部会自动创建一个引用计数器,是系统用来判断是否回收对 ...

  7. 省市区三级-javabean和mybatis

    bean: package com.baiwang.moirai.model.sys; import com.fasterxml.jackson.annotation.JsonInclude; /** ...

  8. 使用buildroot搭建linux文件系统【转】

    本文转载自:http://blog.csdn.net/metalseed/article/details/45423061 (文件系统搭建,强烈建议直接用buildroot,官网上有使用教程非常详细b ...

  9. 浅谈Linux Kernel 中循环链表的实现

    前阵子在弄缓存的时候,我们需要将qemu对于磁盘镜像文件写请求串成一个链表,最终将这个链表里面的写请求全部刷回到镜像文件里面,那么我们便需要一个强健,可靠的链表的接口,于是我们仿照Linux 2.4. ...

  10. linux下离线安装svn服务器并配置

    一.下载相应的包 subversion-1.8.18.tar.gz   下载地址:http://subversion.apache.orgsqlite-autoconf-3190300.tar.gz ...