给定一个含有 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. Office WORD如何输入长下划线

    选中一段文字,点击下划线按钮,可以添加下划线   同样,选中一段空格,点下划线,也可以添加下划线    

  2. 无限级分类Asp.net Mvc实现

    无限级分类Asp.net Mvc实现   无限级分类涉及到异步加载子类.加载当前类和匹配问题,现在做一个通用的实现.   (一) 效果如下:   (二)设计.实现及使用 (1)数据库 (a)表设计db ...

  3. NPOI解析Excel

    园子的文章: http://www.cnblogs.com/csqb-511612371/category/654604.html 关键就是如何解析Excel表头,特别是合并单元格的..还有对应NPO ...

  4. HBase 数据迁移

    最近两年负责 HBase,经常被问到一些问题, 本着吸引一些粉丝.普及一点HBase 知识.服务一点阅读人群的目的,就先从 HBase 日常使用写起,后续逐渐深入数据设计.集群规划.性能调优.内核源码 ...

  5. bash builtin eval

    1 在开始执行eval后面的命令之前eval主要做了哪些事情 1.1 去掉反斜杠的quoting 比如\$ac_optarg,会变成$ac_optarg. 1.2 去掉单引号的quoting 比如: ...

  6. 解决Android 6.0获取wifi Mac地址为02:00:00:00:00:00问题【转】

    本文转载自:http://www.jb51.net/article/128398.htm 这篇文章主要介绍了Android 6.0获取wifi Mac地址为02:00:00:00:00:00的解决方法 ...

  7. YTU 2597: 编程题B-选拔飞行员

    2597: 编程题B-选拔飞行员 时间限制: 1 Sec  内存限制: 128 MB 提交: 131  解决: 35 题目描述 2100年空军选拔高中生飞行学员基本条件要求如下,年龄范围:16-19周 ...

  8. Evernote相关技术介绍——mysql+lucene+tomcat

    Evernote服务 我们的服务由以下几个组件组成. 分片(NoteStore)   分片是Evernote服务的核心单元,用于存储用户的笔记.每个分片最多可以支撑30万个Evernote用户,并包含 ...

  9. codeforces 686B B. Little Robber Girl's Zoo(水题)

    题目链接: B. Little Robber Girl's Zoo //#include <bits/stdc++.h> #include <vector> #include ...

  10. Gerrit代码Review实战

    代码审核(Code Review)是软件研发质量保障机制中非常重要的一环,但在实际项目执行过程中,却因为种种原因被Delay甚至是忽略.在实践中,给大家推荐一款免费.开放源代码的代码审查软件Gerri ...