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. APPCAN MAS接口之AJAX

    1.打开APPCAN IDE,文件→新建→MAS服务 2.新建MAS项目 3.新建MAS接口,访问地址http://mobile.163.com/special/chuangye/ 4.修改if_cy ...

  2. mybaitis uuid插入和定义返回类型

  3. MS Project 使用之创建项目信息

    1. 我们打开MS Project 2013,创建一个空白文档. 2. 切换“项目”选项卡,点击“项目信息”,设置项目开始时间等信息,项目一般是需要设置开始时间和使用日历的,下面我们分别进行设置 如, ...

  4. java中二进制和流的相互转换

    流转二进制 public static byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream ou ...

  5. Java 7 Concurrency Cookbook 翻译 第一章 线程管理之四

    七.创建和运行一个后台线程 Java中有一种特别的线程叫做 deamon(后台) 线程.这类线程具有非常低的权限,并且只有在同一个程序中没有其他的正常线程在运行时才会运行.注意:当一个程序中只剩下后台 ...

  6. Java 的printf(转)

    出处:http://blog.csdn.net/swandragon/article/details/4653600 public class TestPrintf{public static voi ...

  7. Codeforces Round #288 (Div. 2) E. Arthur and Brackets

    题目链接:http://codeforces.com/contest/508/problem/E 输入一个n,表示有这么多对括号,然后有n行,每行输入一个区间,第i行的区间表示从前往后第i对括号的左括 ...

  8. iOS开发——高级篇——地理定位 CoreLocation

    一.CoreLocation 在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆.找KTV.找电影院等等导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达 在上述应用 ...

  9. 热更新脚本C#light,ulua,Scorpio性能比较

    http://www.unity蛮牛.com/thread-32861-1-1.html 测试环境: unity4.5.2  三个脚本全是源码导入  PC :处理器 Intel(R) Core(TM) ...

  10. tab切换,滑动门

    <SCRIPT type=text/javascript>  jQuery(document).ready(function () {          changediv([" ...