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.

题目大意:给定一个非负数组,和一个指定值,找出最小子数组长度使得这个子数组的和大于指定的值。

解题思路:采用滚动计算的思路,类似rolling hash的思想,首先累加数组元素,当大于指定元素时——>执行(从这个子数组的第一个数开始,循环减去这些数),遍历一遍,得到最小子数组长度。

    public int minSubArrayLen(int s, int[] nums) {
if (nums == null||nums.length == 0) {
return 0;
}
int min = 0,pos=0,len=Integer.MAX_VALUE;
for (int i =0; i<nums.length; i++ ) {
min+=nums[i];
while(min>=s){
if (len>i-pos+1) {
len=i-pos+1;
}
min-=nums[pos];
pos++;
}
}
return len==Integer.MAX_VALUE?0:len;
}

最新文章

  1. Entity Framework 6 Recipes 2nd Edition(13-5)译 -&gt; 使POCO的修改追踪更高
  2. SpringBoot中yaml配置对象
  3. iOS-证书相关
  4. Project Woosah Tu (五色土)
  5. Redis学习笔记一:数据结构与对象
  6. mtu
  7. 框架学习笔记:Unity3D的MVC框架——StrangeIoC
  8. SharePoint 2010 获取列表全部定义方法
  9. Python查询MySQL进行远程采集图片实例
  10. Android系统--输入系统(十一)Reader线程_简单处理
  11. Spring学习(17)--- 三种装配Bean方式比较
  12. 关于Ext 修复源代码 bug的方法
  13. (引用)!Unicode,GBK以及UTF8的联系和区别
  14. 区分IE8/IE7/IE6及其他浏览器
  15. 地图api
  16. 洛谷P1099 树网的核
  17. sqllocaldb 2016安装
  18. Node.js实战(二)之HelloWorld示例
  19. Matlab学以致用 - 曲线拟合
  20. [Selenium]怎样等待元素出现之后再消失,譬如Loading icon

热门文章

  1. sql语句游标的写法
  2. table 数据少时 ,tr高度变化
  3. Const和ReadOnly
  4. MySQL 5.6 解决InnoDB: Error: Table &quot;mysql&quot;.&quot;innodb_table_stats&quot; not found.问题
  5. acl操作记录
  6. AppStore上架规则
  7. iOS_ruby环境的配置
  8. Business Intelligence (BI)
  9. ubuntu系统安装的MySql数据库,远程不能访问的几种可能问题
  10. 《chkconfig命令》-linux命令五分钟系列之四