Minimum Size Subarray Sum —— LeetCode
2024-08-28 19:26:40
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;
}
最新文章
- Entity Framework 6 Recipes 2nd Edition(13-5)译 ->; 使POCO的修改追踪更高
- SpringBoot中yaml配置对象
- iOS-证书相关
- Project Woosah Tu (五色土)
- Redis学习笔记一:数据结构与对象
- mtu
- 框架学习笔记:Unity3D的MVC框架——StrangeIoC
- SharePoint 2010 获取列表全部定义方法
- Python查询MySQL进行远程采集图片实例
- Android系统--输入系统(十一)Reader线程_简单处理
- Spring学习(17)--- 三种装配Bean方式比较
- 关于Ext 修复源代码 bug的方法
- (引用)!Unicode,GBK以及UTF8的联系和区别
- 区分IE8/IE7/IE6及其他浏览器
- 地图api
- 洛谷P1099 树网的核
- sqllocaldb 2016安装
- Node.js实战(二)之HelloWorld示例
- Matlab学以致用 - 曲线拟合
- [Selenium]怎样等待元素出现之后再消失,譬如Loading icon
热门文章
- sql语句游标的写法
- table 数据少时 ,tr高度变化
- Const和ReadOnly
- MySQL 5.6 解决InnoDB: Error: Table ";mysql";.";innodb_table_stats"; not found.问题
- acl操作记录
- AppStore上架规则
- iOS_ruby环境的配置
- Business Intelligence (BI)
- ubuntu系统安装的MySql数据库,远程不能访问的几种可能问题
- 《chkconfig命令》-linux命令五分钟系列之四