【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 ...
随机推荐
- 如何正确学习JavaScript
不要这样学习JavaScript 不要一开始就埋头在成堆的JavaScript在线教程里 ,这是最糟糕的学习方法.或许在看过无数个教程后会有点成效,但这样不分层次结构地学习一个东西实在是十分低效,在实 ...
- String与StringBuilder
package com.wangzhu.string; /** * String类是final类,也就是说String类不能被继承,并且其成员方法都默认为final方法.<br/> * * ...
- 【Linux高频命令专题(14)】nl
概述 nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样,nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 ...
- SpringMVC学习总结(六)——SpringMVC文件上传例子(2)
基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下使用SpringMVC进行表单上的文件上传以及多个文件同时上传的不同方法 一.配置文件: SpringMVC 用的是 的 ...
- GuessNumber
import java.util.*; public class GuessNumber { public static void main(String[] args) { int num = ne ...
- login.java
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Login extends JFrame ...
- windows环境下安装 zookeeper
我们下载下来的zookeeper的安装包是.tar.gz格式的,但是还是可以在windows下运行. 下载地址 http://mirrors.hust.edu.cn/apache/zookeeper/ ...
- 对原生态jdbc程序中问题总结
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...
- 比微软kinect更强的视频跟踪算法--TLD跟踪算法介绍
转自:http://blog.csdn.net/carson2005/article/details/7647500 TLD(Tracking-Learning-Detection)是英国萨里大学的一 ...
- MFC多文档中opencv处理图像打开、保存
需要在C**Doc和C**View中进行相应修改 图像打开: Doc.cpp中: BOOL CCVMFCDoc::Load(IplImage** pp, LPCTSTR csFilename) { I ...