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(最小子数组的和)的更多相关文章

  1. [LintCode] Minimum Size Subarray Sum 最小子数组和的大小

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  2. [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 ...

  3. 【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 ...

  4. 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 ...

  5. LeetCode 209 Minimum Size Subarray Sum

    Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...

  6. 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 ...

  7. [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 ...

  8. leetcode面试准备:Minimum Size Subarray Sum

    leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...

  9. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  10. 【刷题-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 ...

随机推荐

  1. Pycharm中SQL语句提示SQL Dialect is Not Configured

    解决办法: 在File---->Setting--->Languages & Frameworks--->SQL Dialects中,选择对应的数据库,如MySQL,之后点击 ...

  2. Java基础—序列化与反序列化(转载)

    转载自: Java序列化与反序列化 1.Java序列化与反序列化 Java序列化是指把Java对象转换为字节序列的过程:而Java反序列化是指把字节序列恢复为Java对象的过程. 2.为什么需要序列化 ...

  3. Python基础-set集合

    1.集合的创建 s = set('fansik and fanjinbao') print(s) 打印结果(去掉了重复的字符):{'k', 'd', 'f', 'n', ' ', 'j', 'i', ...

  4. 【转】python面向对象中的元类

    type() 动态语言和静态语言最大的不同,就是函数和类的定义,不是编译时定义的,而是运行时动态创建的. 比方说我们要定义一个Hello的class,就写一个hello.py模块: class Hel ...

  5. Python 7 多线程及进程

    进程与线程: 进程的概念: 1.程序的执行实例称为进程. 2.每个进程都提供执行程序所需的资源.一个进程有一个虚拟地址空间.可执行代码.对系统对象的开放句柄.一个安全上下文.一个独特的进程标识符.环境 ...

  6. 40个你可能不知道的Python的特点和技巧

    1.拆箱 >>> a, b, c = 1, 2, 3 >>> a, b, c (1, 2, 3) >>> a, b, c = [1, 2, 3] ...

  7. 基于SSM的单点登陆03

    TbUser.java和TbUserExample.java,TbUserMapper.java,TbUserMapper.xml由mybatis框架生成. generatorConfig.xml & ...

  8. 大话设计模式之PHP篇 - 单例模式

    在编写PHP代码的时候,经常使用new关键字实例化一个对象,比如 <?php Class Database { } $db = new Database; 这是最常规的实例化操作方法,像数据库操 ...

  9. MySQL数据库基本操作(三)

    MySQL补充: mysql是关系型数据库,关系数据库,是建立在关系模型基础上的数据库,现实世界中的各种实体,以及实体之间的各种联系,均用关系模型(table)来表示.#关系模型就是指二维表格模型,因 ...

  10. linux 安装tomcat7

    wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.81/bin/apache-tomcat-7.0.81.tar.gz 解压安装包 t ...