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 ...
随机推荐
- 字节流和字符流 in Java
@0: 深入理解Java中的流(Stream) @1:字节流:java.io.InputStream:public abstract class InputStreamThis abstract cl ...
- 指定Python线程数目
可以通过threading.semaphore()来指定并行运行的Python线程的数目. #!/usr/bin/python2.7 #File: threadsNum.py #Author: lxw ...
- 在python中是如何管理内存的
python有一个私有堆空间来保存所有的对象和数据结构.作为开发者,我们无法访问它,是解释器在管理它,但是有了核心api后,我们可以访问一些工具.python内存管理器控制内存分配 内置垃圾回收器会回 ...
- asp.net 利用Response.Filter 获取输出内容, 变更输出内容
重写 Response.Filter 就可以获取或更新输出到浏览器的内容 资料: https://weblog.west-wind.com/posts/2009/Nov/13/Captur ...
- loadrunder脚本篇——文件读写操作
函数说明 函数原型: size_t fwrite( const void *buffer, size_t size, size_t count, FILE *file_pointer ); 参数说明 ...
- UI控件之UIPickerView的协议方法
UIPickerView:选择视图,父类是UIView UIPickerView *pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(1 ...
- 把Android原生的View渲染到OpenGL Texture
http://blog.csdn.net/u010949962/article/details/41865777 最近要把Android 原生的View渲染到OpenGL GLSurfaceView中 ...
- OpenSSL for Android
http://blog.csdn.net/xiongmc/article/details/25736041 OpenSSL1)开源项目Guardian Project试图让Android手机也拥有类似 ...
- linux du与ls查看文件大小时的区别
du和ls查看文件大小的区别 du == disk usage (磁盘使用量,占用的磁盘空间)du 的基本使用du -s #s参数是可以统计占硬盘空间大小的如 du -skh web-k或-- ...
- String和StringBuilder、StringBuffer
Java平台提供了两种类型的字符串:String和StringBuffer/StringBuilder String 只读字符串,这里的只读并不是指String类型变量无法被修改,而是指String类 ...