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 ...
随机推荐
- 制作 macOS High Sierra U盘
制作 macOS High Sierra U盘USB启动安装盘方法教程 (全新安装 Mac 系统) 随着苹果 macOS High Sierra 正式版发布,很多使用 Mac 电脑的同学都已升级到最新 ...
- docker报错: x509: certificate has expired or is not yet valid
环境:最小化安装centos7 问题:docker 启动没问题,但是查询 镜像时报错 Error response from daemon: Get https://index.docker.io/v ...
- 使用MySQL Workbench查询超时的错误
MySQL Workbench是MySQL提供的连接工具,一直在用它.但是今天运行了一个SQL缺报出如下的错误: errcode 2013 lost connection to mysql serve ...
- Linux上的VirtualBox如何使用USB
问题: VirtualBox设置中已经启用了"USB2.0(EHCI)控制器"选项,但是虚拟机中始终没有USB设备,右下角的状态也显示没有连接任何USB设备,看起来像是Virtua ...
- 【css对齐】块内或者行内图片与文字居中对齐最靠谱的方式!
块内或者行内图片与文字居中对齐最靠谱的方式! 做图片与文字在一行的按钮时候最常用到,总结了一个靠谱的方法,终于可以完美的对齐下面给个代码 首先是html: <p class="btnU ...
- leetcode-12双周赛-1243-数组变换
题目描述: 自己的提交: class Solution: def transformArray(self, arr: List[int]) -> List[int]: if len(arr) & ...
- Java——接口interface
3.5接口interface ①有时必须从几个类中派生出一个子类,继承它们所有的属性和方法.但是,Java不支持多重继承.有了接口,就可以得到多重继承的效果. ②接口(interface)是抽象方法和 ...
- Spring Security + JWT学习
开胃:Oauth2认证流程分析 现在第三方登录已经很普遍了,随便哪个App都会有使用微信登录,使用手机号码登录,或者使用支付宝登录等功能... 下面我们就以使用微信登录,做一个简单的流程分析分析 开胃 ...
- 【dart学习】-- Dart之函数
1. 指定返回值得函数 /** * 无返回值的函数 * params: 可以是任意类型(var和Object类型也可以任意类型). 当然这里的参数类型你可以随意指定我这里已dynamic为例 * 参数 ...
- webpack 添加eslint代码审查
1). 添加包 npm install eslint --save-dev npm install eslint-loader --save-dev npm install eslint-plugin ...