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 ...
随机推荐
- django路由系统之反向生成url
from niubin.service import v1 from django.urls import reverse from django.shortcuts import HttpRespo ...
- Python基础(19)_异常处理
一.异常处理 错误和异常: 1.错误的种类: 1)语法错误:这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正 2)逻辑错误: 例如: res1=1/0 .es2=1+'str ...
- Android:日常学习笔记(6)——探究活动(4)
Android:日常学习笔记(6)——探究活动(4) 活动的启动模式 standard模式 standard是活动默认的启动模式,在不进行显示定义的情况下,所有活动都会自动使用这种启动模式. stan ...
- 流量分析系统---echarts模拟迁移中 ,geocoord从后台获取动态数值
由于在echarts的使用手册中说了 {Object} geoCoord (geoCoord是Object类型) ,所以不能用传统的字符串拼接或数组的方式赋值.在后台的controller中用Map& ...
- windows下客户端开发hdf--环境搭建
1.引入依赖 <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop- ...
- linux中环境变量的用法
Linux操作系统下三种配置环境变量的方法[转] 在linux下做开发首先就是需要配置环境变量,下面以配置java环境变量为例介绍三种配置环境变量的方法. 1.修改/etc/profile文件 如果你 ...
- jsp导出
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- linux alsa pcm(此pcm非硬件pcm接口)
转:https://blog.csdn.net/crycheng/article/details/7095899 CODEC :音频芯片的控制,比如静音.打开(关闭)ADC(DAC).设置ADC(DA ...
- python中初始化实例属性
虽然我们可以自由地给一个实例绑定各种属性,但是,现实世界中,一种类型的实例应该拥有相同名字的属性.例如,Person类应该在创建的时候就拥有 name.gender 和 birth 属性,怎么办? 在 ...
- problem-1003(恢复一下)
问题: Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequenc ...