209. Minimum Size Subarray Sum【滑动窗口】
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
Example:
Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
Follow up:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
class Solution {
public int minSubArrayLen(int s, int[] nums) {
if(nums.length == 0 || nums == null) return 0;
int n = nums.length;
int left = 0, sum = 0, res = Integer.MAX_VALUE;
for(int i=0; i<n; i++){
sum += nums[i];
while(sum >= s){
res = Math.min(res, i-left+1);
sum -= nums[left++];
}
}
return res == Integer.MAX_VALUE ? 0 : res;
}
}
209. Minimum Size Subarray Sum【滑动窗口】的更多相关文章
- 【刷题-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] 209. Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- 209. Minimum Size Subarray Sum(双指针)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- [刷题] 209 Minimum Size Subarray Sum
要求 给定一个含有 n 个正整数的数组和一个正整数 s 找出该数组中满足其和 ≥ s 的长度最小的连续子数组 如果不存在符合条件的连续子数组,返回 0 示例 输入:s = 7, nums = [2,3 ...
- LeetCode OJ 209. 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 ...
- LeetCode 209 Minimum Size Subarray Sum
Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- Java for LeetCode 209 Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
随机推荐
- PAT甲级——A1150 TravellingSalesmanProblem【25】
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- jQuery部分疑问及小结
2015/12/28 判断浏览器版本和类型 var mode = document.documentMode || 0;(jquery1.9.1不支持ie 8,9,10) var setExpr = ...
- C/C++ 吐槽第一期:你最讨厌的C/C++里面的数据类型是什么
C/C++ 这里面讨论的范围包括从以往开始,到现有的所有官方标准,VC扩展,GCC扩展, C语言部分包括C89.C90.C99.C11这些知名的大版本,中间或者之前的比如K&R这种不出名的小版 ...
- Javascript高级程序设计--读书笔记之面向对象(一)
哈哈哈万物皆对象,终于到了js的面向对象篇. 一.属性类型 (1)数据属性 数据属性包含一个数据值的位置,在这个位置可以写入和读取数值,数据属性有四个描述器行为的特性 [[Configurable]] ...
- 【教程】CentOS 7安装 最新版本Docker
博主最近需要安装Docker,步骤如下: Docker安装官方地址:https://docs.docker.com/install/linux/docker-ce/centos/ 以下命令都是在roo ...
- BZOJ 3546 [ONTAK2010]Life of the Party (二分图最大匹配必须点)
题解:给出一个二分图,问你取点哪个点会使得二分图匹配数减少. 解法:其实就是问二分图匹配的必须点.先对初始二分图做一次最大匹配. 现在考虑左边点,看残余网络上的边重新构图:如果是匹配边那么就从右往左连 ...
- iftop简单使用
在linux下想查看当前与主机通信的IP有哪些?流量多少?怎么办?使用iftop吧,小巧实用的小工具.在排查问题的时候能起到大作用. centos安装 yum install iftop 界面如下: ...
- mybatis 多表查询sql
在使用spring,spring mvc,mybatis时,mybatis链接数据库做多表查询的时候,sql语句中直接使用left join等链接字符就可以 链接多个表,参数类型是parameterT ...
- php操作redis--字符串篇
前提:已经安装好了redis和相关拓展 常用函数:set/get/decr/incr等 应用场景:普遍的key/value存储类型 连接: $redis = new Redis(); $redis-& ...
- 如何修改magento分类页面的产品的显示个数
经常的有客户问,怎么修改分类页面的产品的个数 这个是magneto后台操作的设置问题 打开后台,在英文状态下: system-->configuration 进入后,点击catalog Prod ...