[LC] 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.
class Solution {
public int minSubArrayLen(int s, int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int res = Integer.MAX_VALUE, start = 0, sum = 0;
for(int i = 0; i < nums.length; i++) {
sum += nums[i];
while (start <= i && sum >= s) {
res = Math.min(res, i - start + 1);
sum -= nums[start++];
}
}
return res == Integer.MAX_VALUE ? 0 : res;
}
}
[LC] 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 ...
- 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
Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- 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 解题报告(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 ...
- 209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
随机推荐
- javaweb03 javaservlet基础一
1.使用JavaEE版的eclipse开发动态的WEB工程(JavaWEB 项目)1).把开发选项切换到JavaEE2).可以在window -> Show View 中找到Package Ex ...
- awk 中 RS,ORS,FS,OFS 区别与联系
一,RS与ORS 1,RS是记录分隔符,默认的分隔符是\n,具体用法看下 [root@krlcgcms01 mytest]# cat test1 //测试文件 111 222 333 444 ...
- jquery 第一节 什么是jQuery
简单来说,jQuery就是javascript的一个框架,也可以说是javascript的一个库.
- 收藏基本Java项目开发的书
一.Java项目开发全程实录 第1章 进销存管理系统(Swing+SQL Server2000实现) 第2章企业内部通信系统(Swing+JavaDB实现) 第3章 企业人事管理系统( Swing+H ...
- np.newaxis 为 numpy.ndarray(多维数组)增加一个轴
>> type(np.newaxis) NoneType np.newaxis 在使用和功能上等价于 None,其实就是 None 的一个别名. 1. np.newaxis 的实用 > ...
- Dynamics CRM - 通过 C# Plugin 来 abandon Business Process Flow
需求说明: 当一个 Entity 存在 Business Process Process 时,有时我们需要改变其状态,在之前写的博客有讲了可以通过 JavaScript 来实现,本篇就来讲一下如何通过 ...
- 2.监控利器nagios手把手企业级实战第一部
1. 什么是Nagios? Nagios是一款开源的网络及服务的监控工具,功能强大,灵活性强,需要注意的是,其服务端只能在linux上面安装. Nagios可以进行分布 ...
- 二、NOSQL之Memcached缓存服务实战精讲第一部
1.Memcached是一套数据缓存系统或软件. 用于在动态应用系统中缓存数据库的数据,减少数据库的访问压力,达到提升网站系统性能的目的:Memcached在企业应用场景中一般是用来作为数据库的cac ...
- JS中的7种设计模式
第九章Refactoring to OOP Patterns 重构为OOP模式 7种设计模式: 1,模版方法模式(template method) 2,策略模式(strategy) 3,状态模式(st ...
- [原]C++新标准之std::chrono::time_point
原 总结 STL 标准库 chrono time_point ratio 概览 类定义 总结 思考 拓展 system_clock steady_clock high_resolution_cloc ...