给定一个含有 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. 谈一谈关于NODE里的N管理

    模块可能与当前的NODE版本不和,NODE升级问题? 一切尽在掌握 1.首先设置好PATH(你安装的目录) Debian系列: sudo gedit /etc/profile Redhat系列: su ...

  2. HDFS的体系架构

    通过对Hadoop分布式计算平台最核心的分布式文件系统HDFS.MapReduce处理过程,以及数据仓库工具Hive和分布式数据库Hbase的介绍.基本涵盖了Hadoop分布式平台的全部技术核心. H ...

  3. thinkphp3.2.3 数据库写入add 方法的一些问题。

    最近在做项目中遇到的一个数据操作add()方法,在不开启debug的模式下会漏掉一些字段没写入数据库. 当时并不知道是这个原因,明明在开发的时候都是没问题的,怎么突然出现这个问题,找了好久都没有头绪, ...

  4. 嵌入式开发之davinci---dm8168VPORT口管脚总结

    http://blog.csdn.net/shanghaiqianlun/article/details/7531365

  5. 2016/4/7 datatype:①json ②XML

    ①JSON 1,postjsonxml.php     json用循环方式处理传来的值  for(key in data) for(var i=0;i<data.length;i++){data ...

  6. Trie(前缀树)和ternary trie和binary search tree

    1 什么是trie trie是一棵多叉树,假如存放的是由26个字母(不区分大小写)构成的字符串的话,那么就是一棵26叉树. trie树是一棵前缀树,因为每个结点只保存字符串中的一个字符,整个字符串保存 ...

  7. Koa2学习(五)中间件

    Koa2学习(五)中间件 Koa2通过app.use(function)方法来注册中间件. 所有的http请求都会依次调用app.use()方法,所以中间件的使用顺序非常重要. 中间件的执行顺序 官方 ...

  8. linux 多个文件中查找字符串 hadoop 3 安装 调试

    http://www.cnblogs.com/iLoveMyD/p/4281534.html 2015年2月9日 14:36:38 # find <directory> -type f - ...

  9. Oracle 表的创建 及相关參数

    1. 创建表完整语法 CREATE TABLE [schema.]table (column datatype [, column datatype] - ) [TABLESPACE tablespa ...

  10. GitHub Pages使用入门

    1.什么是GitHub Pages GitHub Pages可以当做项目的Websites,GitHub Pages有两种最基本的用法: 作为你自己(或者组织)的网站(访问地址示例:http://us ...