Array - 209. Minimum Size Subarray Sum
时间:2022-07-25
本文章向大家介绍Array - 209. Minimum Size Subarray Sum,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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).
思路:
使用sliding window来做,一个指针遍历数组,一个指针记录起始位置。
代码:
java:
class Solution {
public int minSubArrayLen(int s, int[] nums) {
int from = 0, sum = 0, win = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
while (from <= i && sum >= s) {
win = Math.min(win, i - from +1);
sum -= nums[from++];
}
}
return win == Integer.MAX_VALUE ? 0 : win;
}
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 利用scikitlearn画ROC曲线实例
- 小程序微信退款功能实现方法详解【基于thinkPHP】
- PHP回调函数简单用法示例
- PHP explode()函数用法讲解
- 详解提高使用Java反射的效率方法
- PHP fopen函数用法实例讲解
- python正则表达式的懒惰匹配和贪婪匹配说明
- PyQt5-QDateEdit的简单使用操作
- thinkphp5实现无限级分类
- Python numpy矩阵处理运算工具用法汇总
- Django后端分离 使用element-ui文件上传方式
- PHP fprintf()函数用法讲解
- django template实现定义临时变量,自定义赋值、自增实例
- PHP创建文件及写入数据(覆盖写入,追加写入)的方法详解
- PHP写API输出的时用echo的原因详解