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.

思路:用start, end两个游标来记录范围,sum < s end就向后走, s >= sum start就向后走。

我写的代码没有大神的逻辑清晰,先上大神的。

int minSubArrayLen(int s, vector<int>& nums) {
int firstPos = , sum = , minLength = INT_MAX;
for(int i = ; i<nums.size(); i++) { //i即end游标 对所有end游标循环
sum += nums[i];
while(sum >= s) { //对每个end游标的start游标循环 firstPos即为start游标 只有s >= sum 时才把start向后移
minLength = min(minLength, i - firstPos + );
sum -= nums[firstPos++];
}
} return minLength == INT_MAX? : minLength; //没找到s >= sum 时返回0
}

我的代码乱一点,但是也AC了。

int minSubArrayLen(int s, vector<int>& nums) {
int start = , end = ;
int sum = ;
int minLength = nums.size() + ;
while(end <= nums.size()) //有等于是因为结尾到最后面时 起始点还可能移动
{
if(sum < s)
{
if(end == nums.size()) break;
sum += nums[end++];
}
else
{
minLength = (minLength < (end - start)) ? minLength : (end - start);
sum -= nums[start++];
}
}
minLength = (minLength == nums.size() + ) ? : minLength; //没找到符合条件的子序列 返回0
return minLength;
}

【leetcode】Minimum Size Subarray Sum(middle)的更多相关文章

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

  2. 【数组】Minimum Size Subarray Sum

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

  3. 【leetcode】Search for a Range(middle)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  4. 【leetcode】Evaluate Reverse Polish Notation(middle)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  5. 【leetcode】Container With Most Water(middle)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  6. 【leetcode】Swap Nodes in Pairs (middle)

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  7. 【leetcode】Linked List Cycle II (middle)

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  8. 【leetcode】Reverse Linked List II (middle)

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  9. 【leetcode】Binary Search Tree Iterator(middle)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. JCarouselLite--帮助文档

    jcarousellite是一款jquery插件,可以控制文档元素滚动,丰富的参数设置可以控制滚动的更多细节,是一款不可多得的滚动插件. ------------------ 官网地址:http:// ...

  2. C#3.0 特性

    C#3.0特性 隐式类型的本地变量和数组 对象初始值设定项 集合初始值设定项 扩展方法 匿名类型 lambda表达式 查询关键字 自动实现的属性 分布方法定义 lambda表达式与表达式树 https ...

  3. VBA 实现批量excel文件复制

    对于每天要将文件复制到其他多个路径 1 在test文件下新建3个子文件夹 test1,test2,test3 2 在test1下新建li01.xlsx,li02.xlsx,hua01.xlsx,hua ...

  4. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

  5. oracle查询单表占用空间的大小

    oracle查询单表占用空间的大小 SELECT segment_name AS TABLENAME, BYTES B, BYTES KB, BYTES MB FROM user_segments w ...

  6. leofs存储总结

    1.leofs角色 Account(账户).Bucket(对象桶).Object(对象), gateway.manager0.manager1.storage Account 一个account可以创 ...

  7. Java之properties文件读取

    1.工程结构 2.ConfigFileTest.java package com.configfile; import java.io.IOException; import java.io.Inpu ...

  8. javascript高级程序设计---CSS操作

    CSS与JavaScript是两个有着明确分工的领域,前者负责页面的视觉效果,后者负责与用户的行为互动.但是,它们毕竟同属网页开发的前端,因此不可避免有着交叉和互相配合. HTML元素的style属性 ...

  9. FineUI第十五天---表格概述

    Grid表格概述  跟Asp.Net的差不多. 下面介绍一些属性:  ExpandUnusedSpace:此列充满所有的剩余空间,并且随着表格宽度变化而变化 DataToolTipField:用来显示 ...

  10. Entity Framework Code First数据库自动更新2

    以前做项目的时候,没有采用分类库的形式,所以迁移一致非常顺利,没有出现过任何状况. 这次做项目稍微有点大,必须要分类库才方便开发维护. 在解决方案中启用项目EntityFramework迁移时却发生了 ...