【LeetCode 209】Minimum Size Subarray Sum
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.
题意:
求一个给定数组的部分连续和Sum,要求Sum大于给定数值S。返回满足条件的连续和Sum中长度最短的。
思路:
通过标记起始位置(p)、终止位置(q)构建一个滑动“窗口”,使“窗口”内元素的和Sum始终保持小于s。若从(q)端新加元素使得Sum >= s,则更新Sum的最小长度值_min,并从(p)端删除元素,保持Sum < s。
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
const int len = nums.size();
if(len == )
return ;
//初始化,_min初始化为len+1(上界)
int p = , q = , sum = , _min = len + ;
for(; q < nums.size(); q++)
{
//顺序扫描数组,从q端添加元素
sum += nums[q];
//保持“窗口”值小于s
while(sum >= s)
{
if((q - p) < _min)
_min = q - p;
//从p端删除元素
sum -= nums[p++];
}
}
return _min > len ? : _min + ;
}
};
【LeetCode 209】Minimum Size Subarray Sum的更多相关文章
- leetcode面试准备:Minimum Size Subarray Sum
leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...
- 【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 ...
- 【数组】Minimum Size Subarray Sum
题目: Given an array of n positive integers and a positive integer s, find the minimal length of a sub ...
- LeetCode OJ:Minimum Size Subarray Sum(最小子数组的和)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【刷题-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 ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 领扣-209 长度最小的子数组 Minimum Size Subarray Sum MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [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 ...
随机推荐
- hibernate.cfg.xml hibernate 配置文件模板
<?xml version='1.0' encoding='UTF-8'?> <!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition ...
- CTSC2016&&APIO2016游记
4.30 下午衡中放假,我们因为比赛的缘故提前到中午12:00放假 然后我爸爸说要来接我,直到下午两点多他才到,然后衡中宿舍的楼管阿姨死活不给我开门 莫名其妙的等到了三点多快四点的时候我才跟实验班的一 ...
- http报文在网络中是明文传输的,所以不安全。HTtp必然来临
HTTP数据在网络中裸奔 HTTP明文协议的缺陷,是导致数据泄露.数据篡改.流量劫持.钓鱼攻击等安全问题的重要原因.HTTP协议无法加密数据,所有通信数据都在网络中明文“裸奔”.通过网络的嗅探设备及一 ...
- Hibernate逍遥游记-第15章处理并发问题-003乐观锁
1. 2. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; drop table if exists ...
- 6、JPA_映射单向多对一的关联关系(n的一方有1的引用,1的一方没有n的集合属性)
单向多对一的关联关系 具体体现:n的一方有1的引用,1的一方没有n的集合属性 举个例子:订单Order对顾客Customer是一个单向多对一的关联关系.Order是n的一方,有对Customer的引用 ...
- ubuntu 映射网络驱动器到本地
公司办公都是在ubuntu服务器上,所以每每拷贝修改文件都要ftp之类的,实在不方便. 索性将服务器挂载到自己本地目录下. 服务器端参考其他samba安装和配置.这里只是说下本地自动挂载方法. 一.首 ...
- Django admin的一些有用定制
Model实例,myapp/models.py: from django.db import models class Blog(models.Model): name = models.CharFi ...
- mysql字符串区分大小写的问题
一.1. CREATE TABLE NAME(name VARCHAR(10)); 对这个表,缺省情况下,下面两个查询的结果是一样的: SELECT * FROM TABLE NAME WHERE n ...
- NDK(21)JNI的5大正确性缺陷及优化技巧(注意是正确性缺陷)
转自 : http://www.ibm.com/developerworks/cn/java/j-jni/index.html JNI 编程缺陷可以分为两类: 性能:代码能执行所设计的功能,但运行缓慢 ...
- launch genymotion simulator from command line
Command to launch genymotion headless - player --vm-name Nexus_4 if player is not already added to p ...