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)的更多相关文章
-
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 ...
-
【数组】Minimum Size Subarray Sum
题目: Given an array of n positive integers and a positive integer s, find the minimal length of a sub ...
-
【leetcode】Search for a Range(middle)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
-
【leetcode】Evaluate Reverse Polish Notation(middle)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
-
【leetcode】Container With Most Water(middle)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
-
【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 ...
-
【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 ...
-
【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-> ...
-
【leetcode】Binary Search Tree Iterator(middle)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
-
Cdnbes负载均衡的权重用法解释
(1)相同域名添加两条记录,解析不同的ip,可以设置权重,比如权重2,就意思占百分之20 ,数字越大,优先级越大 (2)这个hash 如果用户访问的源是挂掉的.会去第二个源
-
std的find和reverse_iterator联合使用
上代码: // test2013.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdlib.h> #in ...
-
关于时间的util类,以后方便查阅
public static int lastDayOfMonth(int year, int month) { if (month == 2) { if (isLeapYear ...
- 【英语】Bingo口语笔记(55) - work系列
-
ref 参数
当使用ref 作为参数赋值时,ref 得需要初始化,就是在从新定义一下 参数的值,下面有列子: 在控制台中运行如下: //定义一个方法,两个参数 i和a . public static void ge ...
-
java filter的一些理解
java filter即 java中的过滤器: 一. * web项目中只有三个组件 * 过滤器filter ↓ 级 别 * 监听器 ↓ 级 别 * servlet ...
-
基于cx_freeze编译PyQt4程序(numpy &; scipy)
当开发完成PyQt4程序后,需要提供给他人使用,这时最好的办法是将Python程序编译成exe文件. 通常我采用cx_freeze完成这个工作,即编写setup.py文件,执行python setup ...
-
c语言结构体2之变量赋值于字符串
#include <stdio.h> #include <stdlib.h> struct dangdang { ]; ]; ]; int num; int bugnum; ] ...
-
Linux系统启动过程介绍
Linux系统启动过程介绍 学习操作系统有必要了解一下系统的启动过程,这样在面对各种系统故障的时候能快速定位解决问题,下面以Centos来分析linux系统的启动过程. 1.BIOS自检:当开机的时候 ...
-
8Manage:“消费升级”缘何剑指企业一体化管理变革?
[导读]提到消费升级,大家都会想起美学.个性化.品质等标签,近年来经济发展所伴随的消费需求转型在逐渐凸显,开始从粗狂型到精细化,如:关注产品性价比.服务个性化等内容.企业在消费升级下应该如何应对呢?8 ...