题目:

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. udacity android 实践笔记: lesson 4 part b

    udacity android 实践笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

  2. Unix系统编程():分散输入和集中输出(Scatter-Gather IO):readv和writev

    分散输入和集中输出(Scatter-Gather IO):readv和writev 请问这个v又代表什么? readv和writev系统调用分别实现了分散输入和集中输出的功能. #include< ...

  3. [shell]C语言调用shell脚本接口

    Use popen if you want to run a shell command and want the parent process to be able to talk to the c ...

  4. 4种方法让SpringMVC接收多个对象 <转>

    问题背景: 我要在一个表单里同时一次性提交多名乘客的个人信息到SpringMVC,前端HTML和SpringMVC Controller里该如何处理? 第1种方法:表单提交,以字段数组接收: 第2种方 ...

  5. DDR2基础

    一. DDR2介绍 DDR2由JEDEC(电子设备工程联合委员会)开发的新生代内存技术标准.该标准定义了DDR2封装.寻址及操作.电气等所有特性. DDR相关技术对比 DDR DDR2 DDR3 电压 ...

  6. 02 java 程序环境

    java 安装与设置 1. 安装 jdk 2. 设置执行路径 unix: set path=(/usr/local/jdk/bin $path) ( 在~/.cshrc文件里增加) linux: ex ...

  7. SQL on Hadoop系统的最新进展(1)

    转自:http://blog.jobbole.com/47892/ 为什么非要把SQL放到Hadoop上? SQL易于使用.那为什么非得基于Hadoop呢?the robust and scalabl ...

  8. vim 编辑器使用技巧

    看着李立鹏熟练的使用vim,哥心里痒痒的,也来试试! vim pkf.txt i 插入编辑 esc -> :x //退出保存 f8 ->//进入多窗口模式 以下是其他命令: 命令历史 以: ...

  9. [转]Tcpcopy简介与实战

    Tcpcopy简介 TCPCopy是一种请求复制(所有基于tcp的packets)工具 ,其功能是复制在线数据包,修改TCP/IP头部信息,发送给测试服务器,达到欺骗测试服务器的TCP 程序的目的,从 ...

  10. hdu4734(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 思路:记忆化搜索. #include<iostream> #include<c ...