当前位置:网站首页>LeetCode 209. 长度最小的子数组
LeetCode 209. 长度最小的子数组
2022-07-02 11:26:00 【HumbleFool】
LeetCode 209. 长度最小的子数组
前缀和 + 二分
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int n = nums.size();
int s[n + 1] = {
0};
for(int i = 1; i <= n; i ++) s[i] = s[i - 1] + nums[i - 1];
int res = 1e9;
for(int i = 1; i <= n; i ++)
{
int t = s[i] - target;
int l = 0, r = n;
while(l < r)
{
int mid = (l + r + 1) >> 1;
if(s[mid] <= t) l = mid;
else r = mid - 1;
}
if(s[r] <= t) res = min(res, i - r);
}
return res == 1e9 ? 0 : res;
}
};
滑动窗口 / 双指针
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int sum = 0, res = 1e9;
for(int l = 0, r = 0; r < nums.size(); r ++)
{
sum += nums[r];
while(sum >= target)
{
res = min(res, r - l + 1);
sum -= nums[l ++];
}
}
return res == 1e9 ? 0 : res;
}
};
边栏推荐
- 没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
- Tmall product details interface (APP, H5 end)
- Chapter 9: xshell free version installation
- The use of TestNG, the testing framework (II): the use of TestNG XML
- STM32标准固件库函数名记忆(二)
- 篇9:XShell免费版安装
- 3. Function pointers and pointer functions
- Find the maximum inscribed circle of the contour
- Fabric. JS dynamically set font size
- STM32库函数进行GPIO初始化
猜你喜欢

MQ tutorial | exchange (switch)

复用和分用

STM32库函数进行GPIO初始化

Factal: Unsafe repository is owned by someone else Solution

JMeter script parameterization

使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式

< schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv

It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen

< schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621

【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
随机推荐
Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
Check password
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
buuctf-pwn write-ups (7)
Fabric.js 手动加粗文本iText
复用和分用
< schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621
buuctf-pwn write-ups (7)
Pychart connects to the remote server
The use of TestNG, the testing framework (II): the use of TestNG XML
STM32-DAC实验&高频DAC输出测试
求轮廓最大内接圆
tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
Fabric.js 橡皮擦的用法(包含恢复功能)
Fabric. JS upper dash, middle dash (strikethrough), underline
The evolution process of the correct implementation principle of redis distributed lock and the summary of redison's actual combat
Fabric.js 上划线、中划线(删除线)、下划线
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享