LeetCode OJ:Minimum Size Subarray Sum(最小子数组的和)
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.
给一个数组以及一个数字,求满足大于该数字的最小的连续的数组元素个数的最小值。
代码写的比较乱。具体的思想就是用两个指针,一个先向前走, 当相加之和大于s的时候,将另一个指针也向前走,并减去相应的数字,当小于的时候将元素的个数存入数组,代码如下:
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int sz = nums.size();
vector<int> ret;
if(sz == ) return ;
int i = ;
int j = ;
int tmpSum = ;
while(j < sz){
for( ; i < sz; ++i){
tmpSum += nums[i];
if(tmpSum >= s)
break;
}
if(tmpSum < s) break; //i已经达到数组的末尾了
for( ; j <= i; ++j){
tmpSum -= nums[j];
if(tmpSum < s)
break;
}
ret.push_back(i - j + );
i++, j++;
}
sz = ret.size();
if(sz == ) return ;
int min = ret[];
for(int i = ; i < sz; ++i){
if(min > ret[i])
min = ret[i];
}
return min;
}
};
java版本代码如下所示,对上面做了一些改进,其实完全用不到上下两个循环的,双指针一次搞定:
public class Solution {
public int minSubArrayLen(int s, int[] nums) {
if(nums.length == 0)
return 0;
int subSum = nums[0];
int ret = Integer.MAX_VALUE;
int p1 = 1, p2 = 0;
while(p2 < p1){
if(subSum < s){ //达不到k,指针前移动或者移动到头直接返回
if(p1 < nums.length){
subSum += nums[p1];
p1++;
}else{
if(ret == Integer.MAX_VALUE)
return 0;
return ret;
}
}else{ //达到k,后指针向前移动并且考虑是否更新指针。
ret = Math.min(ret, p1-p2);
subSum -= nums[p2];
p2++;
}
}
if(ret == Integer.MAX_VALUE) //如果没有找到合适的子数组的话,直接返回0
return 0;
return ret;
}
}
新修改的方法为:
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int min = INT_MAX;
int i = ,j = ;
int currSum = ;
int sz = nums.size();
while(currSum < s && j < sz){
currSum += nums[j++];
}
if(j == sz)
return ;
while(j != sz && i <= j){
if(currSum >= s)
min = min(min, currSum);
while(i < j && currSum >= s){
currSum -= nums[i++];
}
while(j != sz && currSum < s){
currSum += nums[++j];
}
}
return min;
}
};
LeetCode OJ:Minimum Size Subarray Sum(最小子数组的和)的更多相关文章
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [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 ...
- 【leetcode】Minimum Size Subarray Sum(middle)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 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 ...
- LeetCode 209 Minimum Size Subarray Sum
Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- 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 ...
- [LeetCode] 325. 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 ...
- leetcode面试准备:Minimum Size Subarray Sum
leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【刷题-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 ...
随机推荐
- pgAgent设定定时备份
PostgreSQL定时自动备份 简介 PostgreSQL数据库中未提供数据库的定时备份功能,所以需要结合备份和定时job功能来共同实现. 这里我选取了2种定时job方式,crontab是Linux ...
- Django基础(三)_分页器、COOKIE与SESSION、FORM表单
分页器(paginator) 分页器的使用 >>> from django.core.paginator import Paginator >>> objects ...
- 流量分析系统---redis
1\启动redis 方法一: 修改了某些配置,具体步骤惨开下面的内容 [root@mini1 ~]# service redis stop/start 方法二: [root@mini1 bin]#cd ...
- Laravel 调试利器 —— Laravel Debugbar 扩展包安装及使用教程
1.简介 Laravel Debugbar 在 Laravel 5 中集成了 PHP Debug Bar ,用于显示调试及错误信息以方便开发.该扩展包包含了一个 ServiceProvider 用于注 ...
- OC_链表实现队列
@interface Node : NSObject @property(nonatomic,strong)NSString *value; @property(nonatomic,strong)No ...
- Ubuntu 配置NTP Server
Ubuntu安装NTP Server很简单,分位3步走: 第一步:安装NTP root@cephadmin:~/ceph-cluster# apt-get install ntp Reading pa ...
- 使用shell统计字符串出现的次数,并从大到小进行排序显示
- Centos6.5安装glusterfs3.6.2
硬件环境Centos6.5 glusterfs3.6.2 先安装必要的包 yum install flex bison 2. 下载glusterfs3.6.2 wget http://downloa ...
- BufferingForwardingAppender in log4net
https://blog.csdn.net/szx1999/article/details/50073857 7. 写日志会影响系统性能吗? 写日志必然是会消耗一定资源的,而RollingFileAp ...
- shell 计算文件交并差
交集 $ sort a b | uniq -d 并集 $ sort a b | uniq 差集a-b $ sort a b b | uniq -u 文件乱序 cat tmp.txt | awk 'BE ...