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.

代码如下:(超时)

 public class Solution {
public int minSubArrayLen(int s, int[] nums) {
// int min=Integer.MAX_VALUE;
int min=nums.length+1; if(nums.length==0)
return 0; for(int i=0;i<nums.length-1;i++)
{
int sum=nums[i],count=1;
if(sum>=s)
return count;
for(int j=i+1;j<nums.length;j++)
{
sum+=nums[j];
count++;
if(count>=min)
break; if(sum>=s)
{
if(count<min)
min=count;
break;
}
} } if(min==nums.length+1)
return 0; return min;
}
}

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. 209. Minimum Size Subarray Sum(双指针)

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

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

  5. LeetCode 209 Minimum Size Subarray Sum

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

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

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

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

  8. Java for LeetCode 209 Minimum Size Subarray Sum

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

  9. 【Leetcode】209. Minimum Size Subarray Sum

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

随机推荐

  1. DML数据操作语言练习

    --创建表T_HQ_BM2 --create table t_hq_bm2 as select * from t_hq_bm; commit; --添加行内容 --insert into t_hq_b ...

  2. 一个QQ木马的逆向分析浅谈(附带源码)

    程序流程:首先注册自己程序的窗口以及类等一系列窗口操作,安装了一个定时器,间隔为100ms,功能搜索QQ的类名,如果找到就利用FindWindow("5B3838F5-0C81-46D9-A ...

  3. $where $options: 'g','i'

    db.classes.update({"count":{$gt:20}},{$set:{"name":"c4"}},false,false) ...

  4. cssTex

    var head= document.getElementById("head");head.style.cssText="width:200px;height:70px ...

  5. C# 小规模查找集合性能测试

    项目中包含浮点运算,大概每秒 20 - 100 万左右. 其计算结果每秒只包含1000个左右. 因此大量运算是重复性的.程序运行时,cpu 在 3% - 10% 浮动.打算将结果缓存.根据键值索值. ...

  6. win7 web开发遇到的问题-由于权限不足而无法读取配置文件,无法访问请求的页面

    错误一: HTTP Error 500.19 - Internal Server Error配置错误: 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的 (ov ...

  7. CCNA 6.3

    no encapsulation ppp(默认是encapsulation hdlc 在encapsulation ppp 后想要改回 hdlc 除了可用encapsulation hdlc外,还可以 ...

  8. Hibernate xml格式和anno格式 mappedby

    xml配置的时候多对一 一对多的外键可以配置一样,但是anno不太好弄,多这边配完了,一那边用个mappedby"“自己在对方的属性”就可以,不然要建一张中间表.xml的mappedby因为 ...

  9. hql语句理解2

    /* * this.getSession().createQuery("sdfdf").executeUpdate();这里面的query可以是delete,update,inse ...

  10. IOS UITableView的分隔线多出问题

    如题,有时显示UITableView多出部分在页面时,下面会显示处多出的行, 此时应该在UITableView初始化时设置为Group if (_tableView == nil) { _tableV ...