【leetcode】Minimum Size Subarray Sum(middle)
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 ...
随机推荐
- Windows疑难杂症之开机无法显示桌面。
开机无法显示桌面可能有以下两种情况. 1.系统故障或病毒引起explorer.exe无法加载启动. 2.注册表故障造成默认的值不是explorer.exe.(可能是安装了某些软件造成此问题) 3,某开 ...
- 繁华模拟赛day8 牛栏
/* 标称并没有用到题解中提到的那种奇妙的性质,我们可以证明,正常从1开始走的话,需要T次,如何使这个次数减小?题解中提到一个办法,有一步小于n/t,我们考虑这一步,如果把它匀到左右两步中,则可以减小 ...
- 关系型数据库ACID
关系型数据库ACID 一.事务 定义:所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位. 准备工作:为了说明事务的ACID原理,我们使用银行账户及资金管理的案例 ...
- PPPoE名词解释
PPPoE拔号的发现阶段(Discovery): PPPoE的发现阶段一共分为4步. 分别是: PADI(PPPoE Active Discovery Initiation) PADO(PPPoE A ...
- mongoDB- - 2 增、删、改 操作
1.创建数据库 语法:use database 说明:如果database不存在,就会创建database:如果存在就会切换到database 2.查看所有数据库 语法:show dbs; 说明:如果 ...
- 8-IO总结
3. 4. 5.
- Linux下查看nginx安装目录
输入命令行: ps -ef | grep nginx master process后边的目录即是.
- 深入理解 CSS3 弹性盒布局模型
Web 应用的样式设计中,布局是非常重要的一部分.布局用来确定页面上不同组件和元素的尺寸和位置.随着响应式用户界面的流行,Web 应用一般都要求适配不同的设备尺寸和浏览器分辨率.响应式用户界面设计中最 ...
- SQL双重游标(双重循环)--笔记
declare )='', )='', )='' --创建游标 declare @cursor cursor --设定游标欲操作的数据集 set @cursor=cursor for select s ...
- keepalived安装
两台虚拟机 两台配置操作一样 环境配置 [root@lb01 /]# yum -y install openssl openssl-devel [root@lb01 /]# yum -y instal ...