题目:

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return
0 instead.

For example, given the array [2,3,1,2,4,3] and s
= 7
,

the subarray [4,3] has the minimal length under the problem constraint.

代码1:遍历,复杂度O(n^2)

int min(int x,int y)
{
return x<y?x:y;
}
int minSubArrayLen(int s, int* nums, int numsSize)
{
int minlen = 100000;
int count = 0;
int i;
int j;
for(i = 0;i < numsSize;i ++)
{
count+=nums[i];
}
if(count<s)
return 0;
count = 0;
for(i = 0;i<numsSize;i++)
{
count = 0;
for(j = i;j>=0;j--)
{
count += nums[j];
if(s <= count)
{
minlen = min(minlen,i-j+1);
break;
}
}
}
return minlen;
}

C代码2:滑动窗口,复杂度O(n)

int minSubArrayLen(int s, int* nums, int numsSize)
{
int minlen = 100000;
int left = 0;
int right = 0;
int sum = 0;
while (right <= numsSize)
{
if (sum >= s)
sum -= nums[left++];
else
sum += nums[right++];
if (right - left < minlen && sum >= s)
minlen = right - left;
if(right == numsSize && sum < s)
break;
}
return minlen == 100000 ? 0 : minlen;
}

复杂度为O(n*logn)的没想出来,thinking。。。

LeetCode—Minimum Size Subarray Sum的更多相关文章

  1. [LeetCode] Minimum Size Subarray Sum 解题思路

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

  2. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

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

  3. (leetcode)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 Minimum Size Subarray Sum (最短子序列和)

    题意:给一个序列,找出其中一个连续子序列,其和大于s但是所含元素最少.返回其长度.0代表整个序列之和均小于s. 思路:O(n)的方法容易想.就是扫一遍,当子序列和大于s时就一直删减子序列前面的一个元素 ...

  5. leetcode面试准备:Minimum Size Subarray Sum

    leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...

  6. 【刷题-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 ...

  7. [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  8. [LintCode] 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 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

随机推荐

  1. iPhone4 降级6.12教程 无须SHSH 不装插件 不睡死[转载] by 轻鸢

    无shsh降级电脑系统,细节操作等其它影响因素较多,不确保每个人都能成功,楼主发帖前刷机几十次均成功.步骤有些繁琐,按照步骤每一步都正确可保证最后不睡死 注意一下,无SHSH降级都是不完美的,开机需要 ...

  2. 基于jQuery仿淘宝产品图片放大镜代码

    今天给大家分享一款 基于jQuery淘宝产品图片放大镜代码.这是一款基于jquery.imagezoom插件实现的jQuery放大镜.适用浏览器:IE8.360.FireFox.Chrome.Safa ...

  3. H3C路由器和交换机的一些记录

    一.模拟器安装需要先安装winpcap,模拟器的脚本是tcl,使用脚本根据拓扑图可以配置模拟器模拟实际的网路线路和设备.二.和模拟器的连接可以使用超级终端,但是超级终端使用的是双字符,这里使用的是Se ...

  4. C++中变量做数组长度

    在Java中,这是完全可以的,比如我们运行如下程序: package cn.darrenchan.storm; import java.util.Arrays; public class Test { ...

  5. asp.net 省市联级代码

    3种代码 asp.net js 和数据库的 都测试通过了..例子在http://download.csdn.net/download/jine515073/6878157

  6. asp.net 下载的几种方式

    protected void Button1_Click(object sender, EventArgs e)  {  /*  微软为Response对象提供了一个新的方法TransmitFile来 ...

  7. resize2fs命令出现这个错误“resize2fs: Operation not permitted While trying to add group #6656” 有数据的会丢数据

    1. resize2fs命令出现这个错误“resize2fs: Operation not permitted While trying to add group #6656”,并且在/var/log ...

  8. mysql实现经纬度计算两个坐标之间的距离sql语句

    select *,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*(111.86141967773438-latitude)/360),2)+COS(PI()*33.070 ...

  9. 【BZOJ】1631: [Usaco2007 Feb]Cow Party(dijkstra)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1631 看到m<=100000果断用dij(可是好像dij比spfa还慢了在这里?)//upd: ...

  10. Java 执行linux scp 远程获取文件和上传

      需要的jar包:ganymed-ssh2-build210.jar import java.io.ByteArrayOutputStream;import java.io.File;import ...