博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
209. Minimum Size Subarray Sum【滑动窗口】
阅读量:4363 次
发布时间:2019-06-07

本文共 892 字,大约阅读时间需要 2 分钟。

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
= s){ res = Math.min(res, i-left+1); sum -= nums[left++]; } } return res == Integer.MAX_VALUE ? 0 : res; }}

转载于:https://www.cnblogs.com/Roni-i/p/11222485.html

你可能感兴趣的文章
linux-nohup命令
查看>>
[LeetCode OJ] Roman to Integer
查看>>
三次握手和四次挥手
查看>>
Redis的简单动态字符串实现
查看>>
putty network error:software caused connection abort
查看>>
存储过程 <3> 和函数的区别
查看>>
高级service之ipc ADIL用法
查看>>
Django框架-基础篇
查看>>
Leetcode: Binary Tree Maximum Path Sum
查看>>
通过虚拟环境创建并开始一个django
查看>>
关于 input[type="button"] , button
查看>>
Android ViewDragHelper全然解析 自己定义ViewGroup神器
查看>>
c++ 基础 const char* 转 char*
查看>>
JS-- 小细节--你悟到了什么?
查看>>
收款 借贷
查看>>
Gson关于抽象类的序列化与反序列化
查看>>
Java面向对象之类和对象
查看>>
Oracle数据库提权(dba权限执行系统命令)
查看>>
Hydra爆破神器使用
查看>>
java.util.zip.ZipException: duplicate entry(重复依赖多版本的类库)
查看>>