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 ...
随机推荐
- 004-JVM指令集(指令码、助记符、功能描述)
一.JVM指令助记符 1)操作数栈 变量到操作数栈:iload,iload_,lload,lload_,fload,fload_,dload,dload_,aload,aload_ 操作数栈到变量:i ...
- JSP页面传递参数乱码问题整理
1.JSP页面之间传递中文参数乱码 (1).a.jsp中正常传递参数,b.jsp 中 <% String projectName = new String(request.getParamete ...
- 运维角度浅谈MySQL数据库优化
一个成熟的数据库架构并不是一开始设计就具备高可用.高伸缩等特性的,它是随着用户量的增加,基础架构才逐渐完善.这篇博文主要谈MySQL数据库发展周期中所面临的问题及优化方案,暂且抛开前端应用不说,大致分 ...
- 每天一个Linux命令(50)netstat命令
netstat命令用来打印Linux中网络系统的状态信息,可让你得知整个Linux系统的网络情况. (1)用法: 用法: netstat [选项参数] (2)功能: ...
- idea创建git分支
此时只是在本地创建好了分支,修改源代码后add,commit将本地分支提交到远程仓库 分支已创建,其它成员此时就可以从git拉分支
- Centos6.5使用yum安装软件的时候 Another app is currently holding the yum lock; waiting for it to exit...
Loaded plugins: fastestmirror, refresh-packagekit, security Existing . Another app is currently hold ...
- 关于centos7下/etc/sysconfig/目录没有iptables问题
在新买的centos7服务器中想打开防火墙,采用传统centos6的方式用service iptables restart/stop/status 之后报错: 而在/etc/sysconfig/目录下 ...
- 利用Phoenix为HBase创建二级索引
为什么需要Secondary Index 对于Hbase而言,如果想精确地定位到某行记录,唯一的办法是通过rowkey来查询.如果不通过rowkey来查找数据,就必须逐行地比较每一列的值,即全表扫瞄. ...
- MAC 系列 之XCode7.1 + HBuilder MUI 离线打包 ipa 上次application leader 问题:ERROR ITMS - 90032
90032 解决方法:
- shutdown TCP 端口445
一. 协议:TCP 端口:445 二. shutdown /m \\192.168.1.15 -s -t 60 net use \\192.168.1.15\ipc$ 密码 /user:账户 三. g ...